Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualFelix Ungman

Posted 25 October 2012 - 11:59 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.

#3Felix Ungman

Posted 25 October 2012 - 11:57 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 that will provide you with significantly better performance. It should perform about the same as any non-LINQ version.

#2Felix Ungman

Posted 25 October 2012 - 11:56 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 iteration.

That said, I don't think you can write a non-LINQ version of that particular code that will provide you with significantly better performance. It should perform about the same as any non-LINQ version.

#1Felix Ungman

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

PARTNERS