-2

How much slower would it be to make a list and check if it contains a value over doing multiple or statements?

List<char> validvalues = new List<char>
{
    'a', 'b', 'c', 'd'
};
if (validvalues.Contains(value))
{
    //do thing
}

versus

if (value == 'a' || value == 'b' || value == 'c' || value == 'd')
{
    //do thing
}

Is there an even better way to achieve this that I'm missing?

Edit: First of all I apologize for my stupid question. To clarify this is a drastic oversimplification of my use case which involves checking it against 16 characters and then 32 chars, then 48 chars etc.. up to 256 chars. I was thinking I could append 16 characters at a time to the already existing list and use the same list to check multiple times. Obviously creating a list and checking over it would be slower ig I was really asking how much this would be an issue.

SimonUnderwood
  • 469
  • 3
  • 12
  • I believe my question was wrongfully closed because it is not asking the same thing as ["similar question"](https://stackoverflow.com/questions/2075629/fastest-way-to-find-an-item-in-a-list). My question is asking whether using a list.contains() is faster than a bunch of or statements as the condition for an if statement whereas that question was asking what is the fastest way to find an item in a list. – SimonUnderwood Dec 30 '21 at 00:26
  • 1
    even so, your question should stay closed. If you consider manual ifs then the set of values you are searching for is extremely small, which most likely means it doesn't matter in terms of performance. And if you want to say it does matter you need to benchmark to first see if it really is a bottleneck in your application for your usage pattern. And if you indeed find this code is in a critical section and it matters the question is irrelevant, because if you need speedup here you would use the method described in the dupe, not any of the 2 options you ask about. – bolov Dec 30 '21 at 00:50
  • The `if statement` is obviously a lot faster, but might be harder to extend in the future when you have for example 20 values to check. There's no general "what is better" answer to this. And if you're using `contains` you should use a `HashSet` or `Dictionary` – Charles Dec 30 '21 at 00:52
  • So @SimonUnderwood, are you arguing that the question should be downvoted into oblivion due to lack of demonstrated research? If that your suggestion it is super easy to achieve... If that is not your intention please [edit] the question to clarify restrictions on your data and what steps you already done yourself to measure performance (https://www.bing.com/search?q=how+to+benchmark+code+c%23+site%3Astackoverflow.com). Additionally while editing the post clarify that any "hash set" solutions are not applicable for {whatever reason you have}... – Alexei Levenkov Dec 30 '21 at 00:55
  • ... Questions asking for help why one variant of the code is faster using standard benchmarks when you believe it should not be (with some plausible reasoning) fare much better on SO than "benchmark these two pieces of code for me and don't be lazy to provide detailed explanation". – Alexei Levenkov Dec 30 '21 at 00:58
  • Depending on how many items you want to compare against, you should also look at **`HashSet`**. But the only real way to know for your data is to **PROFILE**. – Joel Coehoorn Dec 30 '21 at 01:53
  • Or validChars = new bool[256], and index into it by char values. – David Browne - Microsoft Dec 30 '21 at 01:53

1 Answers1

1

Using a list would probably be marginally slower since it has to create a structure, iterate over its contents, etc. The or would be likely compiled directly to IL that compares against constant values. In exchange for that, though, you get more flexibility to change the list at runtime. But, you could get that with a Set, improving the lookup time significantly in exchange for a slightly more expensive setup time. So it would depend on how big the list is and how many comparisons you do.

That said, the only way to know for sure is to try it both ways and measure it. I suspect that the difference would be insignificant unless you performed these tasks millions of times.

In addition, it's highly unlikely that something as simple as this would be a meaningful bottleneck to any application overall. If the application has any kind of I/O, that is probably more likely to be a bottleneck that looking up a value in a relatively small set.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 2
    And, if you make the Set a private static variable, you'll incur the setup cost just once at startup time. – O. Jones Dec 30 '21 at 01:54
  • This. Unless it's inside a bit of code that's executed very frequently it's not going to matter. When you profile code you'll find the vast majority of time is spent in a small amount of code. Optimizing anything else is generally irrelevant. – Loren Pechtel Dec 30 '21 at 05:15