I've got a Collection with up to 19,000 entries in it that I'm iterating over in a foreach statement. At the beginning of the foreach, I check if a string contains 1 of 2 words and a boolean value, then I either continue or perform some more operations.
foreach (SvnStatusEventArgs e in results) //results being my Collection
{
if ((e.Path.Contains("bin") ||
e.Path.Contains("obj")) && !includeBinObjFolders)
continue;
//Do a bunch of things
}
I wasn't sure if the computer would check the string for either "bin" or "obj", and then check the boolean and maybe it'd realize it didn't matter that the strings contained one of those two 'key words'.
Basically I guess what I'm saying is would the following take a different amount of time to run?
foreach (SvnStatusEventArgs e in results) //results being my Collection
{
if (!includeBinObjFolders &&
(e.Path.Contains("bin") ||
e.Path.Contains("obj")
)
)
continue;
//Do a bunch of things
}
For some reason, I hear a voice in the back of my head telling me that it evaluates the right-most expression first, and works its way left. If so, the first should be more efficient right? I don't have an easy way of testing a collection larger than ~200 files so simply using a timer yields results that are so close I can't confirm if one way is better.
Frankly I think it's highly unlikely end users will encounter more than 500 pieces of data in this collection at most, but theoretically it could happen due to user error.
Edit thanks everyone. I tried searching around before posting here but I forgot the term "short circuiting" in regards to logic so I was having a tough time finding an answer relevant to my wordy title here.
Edit 2 Actually I just went and created a small console application that had a 2 for loops iterating 20,000 times each. One tested the Contains first, the other tested the Bool first. Repeating these two loops 10 times over, it looks like the boolean first takes on average half of a millisecond per 20K iterations. The Contains being evaluated first takes roughly 3 milliseconds per 20K iterations. A small difference indeed!