Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Is the LINQ version faster than the foreach one?
Posted
Comments
deepakdynamite 26-Dec-14 5:49am    
Please elaborate your question...
DamithSL 26-Dec-14 5:56am    
update the question with your foreach or Linq example which you trying to test. have you done any benchmarking/ performance comparison test?
BillWoodruff 26-Dec-14 6:01am    
This is a question that is probably to impossible to answer in the very general way you ask about here.

See:

Marc Gravell: http://stackoverflow.com/a/3156097/133321

Jon Skeet: http://stackoverflow.com/a/1044268/133321

Whoa! That's not a fair question - it's not comparing two similar operations.
foreach is a linear operation: unless it leaves the loop prematurely for some reason, it runs the same code (the loop body) on each element of the collection one after the other.

Linq operations do not do that: in fact Linq operations are not executed until the result is needed - so just comparing a Linq method with the equivalent foreach loop is not valid as the later will always take "n" times the code duration, but the Linq method may take a trivial amount of time to set up the operation and then return immediately without executing the "loop body" code at all.

But, even if you "force" the Linq to execute to completion immediately, it is likely to be marginally slower, (or considerably slower in some cases) because of overheads it introduces "behind the scenes" - and it is very easy to write Linq code (or Linq methods) which take longer than an equivalent foreach if you do not pay attention to what you are doing...

Use what it clearest and most appropriate in the code; only worry about "which is fastest" when you have a concrete example to compare. And then watch how you time it very carefully! :laugh:
 
Share this answer
 
Linq is just a query language. You simply define your query above a set of somethings. Let's not think about deferred execution or possibilities of parallelism, but the key here is that set of something. Linq is not on it's on, we speak about Linq to entities, objects, sql, odb... and many other things for that somebody provides linq interface (provider[^]).

If this porvider written so it implements a faster search/filter than foreach, than you will have it, if not, than not.

If there is a database engine to which the linq query is directed, and the database schema is well designed, and the indexes are there, than the answer is probably yes. If you query an array, than the answer will be no.
 
Share this answer
 
v3
As always with the kind of question: "Which is faster?" --> measure it for your particular case.
In general: you can assume with each level of abstraction to get less optimal code in terms of execution speed (since abstraction usually produces generic code instead of specific code).

So, again: measure it for your particular situation.

Cheers
Andi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900