3

What is the point of using query expressions instead of lambda expressions? It not only slower it even more verbose (see here):

Example (from above link):

QE: var products = from p in northwind.Products where p.Category.CategoryName == "Beverages" select p;
LE: var products = northwind.Products.Where(p => p.Category.CategoryName == "Beverages");

Results (from above link):

QE: 00:00:00.0019557, avg. 00:00:00.0004552
LE: 00:00:00.0000574, avg. 00:00:00.0000133

Is it really worth to have 34x times slower code just for readability?

biox
  • 1,526
  • 5
  • 17
  • 28
  • 1
    I think this may have been discussed already in [this question](http://stackoverflow.com/questions/3914611/comparision-linq-vs-lambda-expression), but there isn't really a conclusion. – Alvin Wong Jun 24 '13 at 04:45
  • 3
    If I could tell people only one thing about LINQ it is: **the result of a query expression is a *query***. It's not **the results of executing the query**. You are discovering that *writing the question* is far faster than *answering the question*, which is not surprising. – Eric Lippert Jun 24 '13 at 05:51

2 Answers2

10

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.

svick
  • 236,525
  • 50
  • 385
  • 514
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Thanks for this insight. It's worth pointing out here that even an 'incredibly quick' query can become significant in a high demand application (consider a case of 500,000 concurrent users), so micro-optimisations at this type of level are a good habit where they can be carried out. – Chris Halcrow Sep 04 '14 at 03:27
1

The compiler translates query expressions into queries that use lambdas. What that means is that the two samples will compile to exactly the same code, so there can be no performance difference.

This means the benchmark you linked to is very wrong (and considering the other mistakes it does, that's not very surprising) and that you should decide between the two forms based on readability.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Probably, whichever is first will take longer to build the query because of caching. Benchmarks will probably show the lambda version quicker if you swapped them around. – Chris Walsh Nov 29 '16 at 16:02