They are the same in the end.
The reason your article's test appears incredibly fast is because of deferred execution. That code isn't actually doing anything at the area they are timing. It will only do something when .ToList()
is called.. or another method that forces evaluation of the query (lambda or otherwise). It's quick to interpret the query (incredibly quick, look at the times you've provided), but it's a whole other beast to actually loop over data when the query gets evaluated.
EDIT:
I just read the article. You'll notice that, according to the author, for
loops are the slowest of all 3 (query expression, method syntax, for
loop). This is incredibly wrong.
How can a basic for
loop be thousands of times slower than a lambda? That just doesn't make sense. A loop is the most basic way of iterating over data. What do lambda's do that is so much more incredibly advanced than a loop?
...they don't. They haven't executed yet. Behold: deferred execution.