Is Linq faster, slower or the same?

It:

Box boxToFind = AllBoxes.FirstOrDefault(box => box.BoxNumber == boxToMatchTo.BagNumber); 

Faster or slower:

 Box boxToFind ; foreach (Box box in AllBoxes) { if (box.BoxNumber == boxToMatchTo.BoxNumber) { boxToFind = box; } } 

Both give me the result I'm looking for ( boxToFind ). This will run on the mobile device that I need to be effective.

+6
performance c # linq
source share
8 answers

It should be about the same, except that you need to call First (or, to match your code, Last ), not Where .
Calling Where will give you a set of matching elements (a IEnumerable<Box> ); you only need one matching item.

In general, when using LINQ, you need to know about deferred execution. In your particular case, it does not matter, since you get one element.

+8
source share

The difference is not important unless you have determined that this particular loop is a performance bottleneck through profiling.

If profiling detects that this is a problem, then you might want to explore alternative storage. Store data in a dictionary that provides faster searches than looping through an array.

+8
source share

If micro-optimization is your thing, LINQ works worse, this is only one article , there are many other posts that you can find.

+2
source share

Micro optimization will kill you.
Finish the entire class first, and then if you have performance issues, start the profiler and check for application hotspots.
Make sure you use the best algorithms you can , then move on to micro optimizations this way.

If you have already done this:
Slow → Fast
LINQ <foreach <dl <is unsafe (the latter option is not recommended). Abstractions will make your code slower, in 95% of cases.

+1
source share

The fastest is when you use to loop. But the difference is so small that you ignore it. This will only matter if you are creating a real-time application, but then for these applications, perhaps C # is not the best choice!

0
source share

If AllBoxes is IQueryable, it can be faster than a loop, because the query can have an optimized Where-operation implementation (for example, indexed access).

0
source share

LINQ is 100% slower

Depending on what you are trying to accomplish in your program, but for the most part this is certainly what I would call LAZY PROGRAMMER CODE ...

You are going to “stand up” substantially if you perform any complex queries, combine, etc ... total pos for these types of functions / methods - just do not use it. If you do this with great difficulty, you will be much happier in the long run ... and productivity will be in the world.

NOTE :

I would definitely not recommend LINQ for any program created for speed / synchronization / calculation tasks (i.e. trading HFT and / or trading AT i-0-i for starters).

TESTED

It took almost 10 seconds to complete the connection in "LINQ" versus <1 millisecond.

0
source share

LINQ vs Loop - Performance Test

 LINQ: 00:00:04.1052060, avg. 00:00:00.0041052 Loop: 00:00:00.0790965, avg. 00:00:00.0000790 

Literature:

http://ox.no/posts/linq-vs-loop-a-performance-test

http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html

-2
source share

All Articles