Are the following Lambda and Linq expressions equivalent in terms of execution paths? I guess I’m wondering if the Linq is going to run differently because it’s going to create an IEnumerable before determining if the enumeration has anything in it whereas the lambda expression will stop on the first digit it finds.
var x = valueToMatch
.Any(c => Char.IsDigit(c));
var y = (from c in valueToMatch
select c).Any(c => Char.IsDigit(c)); here
Thx! Joel
It will run differently, but not in any considerable way. If you use the MSIL Disassembler you will see a slightly different output for the first expression and the second, even with optimizations turned on. You can also look at it using a Reflector (which is a little easier to read).
The second version will basically pass each element through something like:
before it executes the Any(c => Char.IsDigit(c)) expression. So there is indeed a difference.
however, the difference is very small in my opinion.
Testing a list of 10,000 characters being looped over 10,000,000 with each method the first one clocks in around ~125ms while the second method takes ~185ms.