• Create Account

## Will using LINQ hurt engine performance (C#)?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1ATC  Members

Posted 25 October 2012 - 11:09 PM

Hey guys,

I am considering what performance implications the use of LINQ/C# could have on my engine's overall performance. I don't think it's any secret that queries are a bit slower (sometimes significantly slower) that plain old C# code. But I'm wondering how big of an impact that could have in a very specific set of contexts. For instance, I use a dash of LINQ in my "GameComponent" classes for sorting out child components, like so:

[source lang="csharp"]public virtual void Update(GameTime gameTime) { if (!Enabled) return; var components = ( from component in ChildComponents orderby component.UpdateOrder ascending where component.Enabled select component).ToArray(); for (int i = 0; i < components.Length; ++i) components[i].Update(gameTime); if (Updated != null) Updated(this, EventArgs.Empty);}[/source]

The code is definitely very concise and pretty, but how about performance? I'm thinking it's not going to be a bottleneck because the number of "GameComponents" will rarely number more than a few tens at the maximum. And it's my understanding that LINQ queries have grown faster in C# 5.0. When the performance hit is very marginal, I tend to favor very "elegant" code like this; it's easy to read, modify and maintain. Another one of my programmers can take one look at this and instantly know what the code is doing. In that way I think a "dash" of LINQ here and there can be helpful in the overall structure of software.

So the question isn't will there be a performance hit... there definitely will be some penalty to pay for using a bit of LINQ here and there. But is it enough to worry about or lose any sleep over? Should I get rid of all LINQ-stuff and rewrite it in "pure" C#? My question isn't about this one, particular method, GameComponent.Update(GameTime), but about situations like these and the use of LINQ in general...

Regards,

--ATC--
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

### #2Alpheus  GDNet+

Posted 25 October 2012 - 11:12 PM

My immediate answer is "no". I can't see using LINQ especially in .NET 4.0 and above as a performance killer. Even in .NET 3.5, it's pretty good.
External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science

Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley

If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts

Spoiler

### #3Hodgman  Moderators

Posted 25 October 2012 - 11:20 PM

I'm only a beginner with LINQ, but from what I can tell, the order in which you use LINQ statements can have a big impact on what computation is done.
e.g. where filters the number of objects, so it should be done before projection (i.e. select) or sorting.

### #4Alpheus  GDNet+

Posted 25 October 2012 - 11:27 PM

To both of you: what LINQ resources are you using to learn LINQ. I've dabbled at it, but admittedly, I'm certainly not an expert.

Also, do you use query syntax or lambda syntax? Out of curiosity.

Edited by Alpha_ProgDes, 25 October 2012 - 11:28 PM.

External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science

Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley

If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts

Spoiler

Posted 25 October 2012 - 11:28 PM

This is probably an excellent opportunity to profile rather than making a premature optimization. In general LINQ performs reasonably well if used correctly -- especially with the more recent versions of .NET -- but only you can do the testing to find out if it's presenting a bottleneck in your particular case. It certainly isn't something that's known to be such a problem in the majority of cases that you should simply avoid it outright, and it does allow very elegant solutions -- especially given you're only using small portions where it provides a real benefit to readability and isn't used extensively throughout the entire code-base.

I'd suggest you continue to use it for now and consider replacing it later on if you find it's a cause of performance problems.

### #6Felix Ungman  Members

Posted 25 October 2012 - 11:53 PM

There are some caveats when using LINQ, such as understanding how lazy evaluation works (and especially when querying against databases).

For example, if you only iterate thru the query once, you're better of not making an array, but just do a foreach on it, such as:

var components = (
from component in ChildComponents
orderby component.UpdateOrder ascending
where component.Enabled
select component);
foreach (var component in components)
component.Update(gameTime);


If you on the other hand are going to iterate thru it many times, then you better cache it into an array, otherwise it will query and sort it each time you iterate.

That said, I don't think you can write a non-LINQ version of that particular code (i.e. tens of items once every game loop) that will provide you with significantly better performance. It should perform about the same as any non-LINQ version.

Edited by Felix Ungman, 25 October 2012 - 11:59 PM.

openwar  - the real-time tactical war-game platform

### #7ATC  Members

Posted 26 October 2012 - 09:32 AM

Thanks everyone...

I changed the queries so that the "where" filter comes before sorting. It seems logical that it will sift out components that aren't enabled and speed up the sorting that follows. If it doesn't, oh well...only had to copy and paste one line of code so no loss. Of course jbadams is right though, and only actually profiling it will tell the true tale of performance...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

### #8Hodgman  Moderators

Posted 27 October 2012 - 01:56 AM

To both of you: what LINQ resources are you using to learn LINQ. I've dabbled at it, but admittedly, I'm certainly not an expert.

Also, do you use query syntax or lambda syntax? Out of curiosity.

I've just been learning from examples on MSDN and using google for specific questions -- most of these searches bring up a StackExchange page where someone's already asked my question.

I'm using the Lambda syntax at the moment, because I love C# lambdas ;)

### #9osmanb  Members

Posted 27 October 2012 - 07:53 AM

That said, I don't think you can write a non-LINQ version of that particular code (i.e. tens of items once every game loop) that will provide you with significantly better performance. It should perform about the same as any non-LINQ version.

Other than the really obvious improvement of just keeping your component list in sorted order, all the time. What order are they stored in normally? If it's arbitrary, then save yourself the trouble of sorting them every frame... (And you could go further and maintain two collections in the entitiy - one for enabled components, one for disabled). Move components between them on state change. Now you need zero LINQ code because you need to do zero work.

### #10ATC  Members

Posted 27 October 2012 - 04:11 PM

Other than the really obvious improvement of just keeping your component list in sorted order, all the time. What order are they stored in normally? If it's arbitrary, then save yourself the trouble of sorting them every frame... (And you could go further and maintain two collections in the entitiy - one for enabled components, one for disabled). Move components between them on state change. Now you need zero LINQ code because you need to do zero work.

It was just example code, but you're right... and I don't actually sort on every frame. Only if the component list has been changed. But you also have to check (or mark "dirty") for changes to the UpdateOrder property which will require a resort. Still, using LINQ is a quite elegant solution to the problem imho
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

### #11archanian  Members

Posted 29 October 2012 - 09:54 PM

I asked myself this very same question not long ago, whilst building a game with SDL.NET in C#. I had a bunch of LINQ expressions (lambda syntax) sprinkled throughout my code for the same reasons you mention - it is very readable and easy to get a grip on what is going on.

It then got to the point where I needed to find places I could speed up a few hot sections of code, and it turned out that by abandoning LINQ in favour of hand-written alternatives I was able to get quite a big performance boost in several places which made a noticeable FPS difference. This was because I was using these LINQ expressions in places that were being called every frame, which is not ideal. If it's something you're only calling every now and then, I guess it's fine. But for code getting hit every frame, you really want things to be as fast as possible, as you'll inevitably rewrite it later to get the best performance possible.

### #12Oberon_Command  Members

Posted 29 October 2012 - 11:45 PM

I'm only a beginner with LINQ, but from what I can tell, the order in which you use LINQ statements can have a big impact on what computation is done.
e.g. where filters the number of objects, so it should be done before projection (i.e. select) or sorting.

When using the SQL-like syntax (ie. "from n in <collection> where <condition>..." etc.), wouldn't the compiler take care of that kind of thing? Is the C# compiler smart enough to notice that you're doing a filter and a projection together, and do the projection after the filter?

### #13Felix Ungman  Members

Posted 30 October 2012 - 02:07 AM

When using the SQL-like syntax (ie. "from n in <collection> where <condition>..." etc.), wouldn't the compiler take care of that kind of thing? Is the C# compiler smart enough to notice that you're doing a filter and a projection together, and do the projection after the filter?

One really elegant aspect of C#/Linq is that the sql query syntax is really just syntactic sugar. You can roll your own classes with methods having the corresponding signatures. They need not have any relationship with the standard ones and you can still use the query syntax. So the choice of syntax doesn't matter performance wise.

On the other hand, if queries agains a database linq-provider get translated into expression trees. It is up to the linq-provider to do any optimization of this query tree. Different providers do it differently. However, the Linq-to-objects provider doesn't optimize (by design).

openwar  - the real-time tactical war-game platform

### #14Oberon_Command  Members

Posted 30 October 2012 - 03:21 PM

When using the SQL-like syntax (ie. "from n in <collection> where <condition>..." etc.), wouldn't the compiler take care of that kind of thing? Is the C# compiler smart enough to notice that you're doing a filter and a projection together, and do the projection after the filter?

One really elegant aspect of C#/Linq is that the sql query syntax is really just syntactic sugar. You can roll your own classes with methods having the corresponding signatures. They need not have any relationship with the standard ones and you can still use the query syntax. So the choice of syntax doesn't matter performance wise.

So in other words, the compiler doesn't optimize the translation from the query syntax to the lambda syntax?

### #15Nypyren  Members

Posted 30 October 2012 - 03:51 PM

So in other words, the compiler doesn't optimize the translation from the query syntax to the lambda syntax?

The compiler would need to prove that the lambda functions used in the query do not have side effects, and it would also need to understand whether the select/where functions are associative or not (even for user-provided ones). Considering C#'s compile speed is still ridiculously fast, I suspect that might be a little bit beyond what it bothers with...

It would be interesting to examine the output IL to get some proof, though!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.