views:

218

answers:

5

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 will I'm in that shader pass. I am currently using a foreach loop, but wondered if utilising Linq might give me a performance increase?

+2  A: 

I think linq is better to use over foreach loop because it gives you much cleaner and easy to understand code. But linq is slower than foreach. get more go through below article

LINQ vs FOREACH vs FOR Loop Performance

Pranay Rana
+4  A: 

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.

codymanix
+2  A: 

You might get a performance boost if you use parallel Linq for multi cores http://msdn.microsoft.com/en-us/library/dd460688.aspx

mcintyre321
+5  A: 

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.

Marc Gravell
+1  A: 

Linq is slower now, but it might get faster at some point. The good thing about Linq is that you don't have to care about how it works. If a new method is thought up that's incredibly fast, the people at MS can implement it without even telling you and your code would be a lot faster.

More importantly though, Linq is just much easier to read. That should be enough reason.

Jouke van der Maas