开发者

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 perfo开发者_运维知识库rmance conscientious of.


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.


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.


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


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.


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!


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).


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜