Vaccano
Vaccano

Reputation: 82291

Is Linq Faster, Slower or the same?

Is this:

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

Faster or slower than this:

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

Both give me the result I am looking for (boxToFind). This is going to run on a mobile device that I need to be performance conscientious of.

Upvotes: 6

Views: 8810

Answers (8)

BO JAGLES
BO JAGLES

Reputation: 1

LINQ is absolutely 100% slower

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

You are going to essentially "stall-out" if you are performing any complex queries, joins etc... total p.o.s for those types of functions/methods- just don't use it. If you do this the hard/long way you will be much happier in the long run...and performance will be a world apart.

NOTE:

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

TESTED:

It took nearly 10 seconds to complete a join in "LINQ" vs. < 1 millisecond.

Upvotes: 0

Huy Nguyen
Huy Nguyen

Reputation: 928

LINQ vs Loop – A performance test

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

References:

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

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

Upvotes: -2

Rubys
Rubys

Reputation: 3207

Micro optimization will kill you.
First, finish the whole class, then, if you have performance problems, run a profiler and check for the hotspots of the application.
Make sure you're using the best algorithms you can, then turn to micro optimizations like this.

In case you already did :
Slow -> Fast
LINQ < foreach < for < unsafe for (The last option is not recommended).
Abstractions will make your code slower, 95% of the time.

Upvotes: 1

Rauhotz
Rauhotz

Reputation: 8140

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

Upvotes: 0

Samuel Neff
Samuel Neff

Reputation: 74899

The difference is not important unless you've identified that this particular loop as a performance bottleneck through profiling.

If profiling does find it to be a problem, then you'll want to look into alternate storage. Store the data in a dictionary which provides faster lookup than looping through an array.

Upvotes: 9

azamsharp
azamsharp

Reputation: 20068

The fastest is when you are using for loop. But the difference is so small that you are ignore it. It will only matter if you are building a real-time application but then for those applications maybe C# is not the best choice anyway!

Upvotes: 0

Ta01
Ta01

Reputation: 31610

If micro-optimization is your thing, LINQ performs worse, this is just one article, there are a lot of other posts you can find.

Upvotes: 3

SLaks
SLaks

Reputation: 887225

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 items (an IEnumerable<Box>); you only want one matching item.

In general, when using LINQ, you need to be aware of deferred execution. In your particular case, it's irrelevant, since you're getting a single item.

Upvotes: 9

Related Questions