Is a LINQ statement faster than a 'foreach'

2019-01-01 13:34发布

I am writing a Mesh Rendering manager and thought it would be a good idea to group all of the meshes which use the same shader and then render these while I'm in that shader pass.

I am currently using a foreach loop, but wondered if utilising LINQ might give me a performance increase?

8条回答
不流泪的眼
2楼-- · 2019-01-01 13:40

I think LINQ is better to use over a foreach loop, because it gives you much cleaner and easy-to-understand code. But LINQ is slower than foreach. To get more, go through the article LINQ vs FOREACH vs FOR Loop Performance.

查看更多
牵手、夕阳
3楼-- · 2019-01-01 13:41

Why should LINQ be faster? It also uses loops internally.

Most of the times, LINQ will be a bit slower because it introduces overhead. Do not use LINQ if you care much about performance. Use LINQ because you want shorter better readable and maintainable code.

查看更多
谁念西风独自凉
4楼-- · 2019-01-01 13:41

It should probably be noted that the for loop is faster than the foreach. So for the original post, if you are worried about performance on a critical component like a renderer, use a for loop.

Reference: In .NET, which loop runs faster, 'for' or 'foreach'?

查看更多
与风俱净
5楼-- · 2019-01-01 13:42

You might get a performance boost if you use parallel LINQ for multi cores. See Parallel LINQ (PLINQ) (MSDN).

查看更多
孤独寂梦人
6楼-- · 2019-01-01 13:52

LINQ-to-Objects generally is going to add some marginal overheads (multiple iterators, etc). It still has to do the loops, and has delegate invokes, and will generally have to do some extra dereferencing to get at captured variables etc. In most code this will be virtually undetectable, and more than afforded by the simpler to understand code.

With other LINQ providers like LINQ-to-SQL, then since the query can filter at the server it should be much better than a flat foreach, but most likely you wouldn't have done a blanket "select * from foo" anyway, so that isn't necessarily a fair comparison.

Re PLINQ; parallelism may reduce the elapsed time, but the total CPU time will usually increase a little due to the overheads of thread management etc.

查看更多
旧人旧事旧时光
7楼-- · 2019-01-01 14:01

This is actually quite a complex question. Linq makes certain things very easy to do, that if you implement them yourself, you might stumble over (e.g. linq .Except()). This particularly applies to PLinq, and especially to parallel aggregation as implemented by PLinq.

In general, for identical code, linq will be slower, because of the overhead of delegate invocation.

If, however, you are processing a large array of data, and applying relatively simple calculations to the elements, you will get a huge performance increase if:

  1. You use an array to store the data.
  2. You use a for loop to access each element (as opposed to foreach or linq).

    • Note: When benchmarking, please everyone remember - if you use the same array/list for two consecutive tests, the CPU cache will make the second one faster. *
查看更多
登录 后发表回答