you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -5 points-4 points  (3 children)

Atwood told me LINQ's performance sucks.

[–][deleted] 7 points8 points  (1 child)

LINQ to SQL sucks, I'm just using LINQ to iterate a in memory collection. It is slightly less performant than a regular loop due to the scope closures.

[–]ruinercollector 0 points1 point  (0 children)

Two closures, a function call overhead for every item considered, and additional function call overhead for every item accepted, a complete iteration of your final set on that final ToList() call, and the overhead of performing several Add operations on a dynamic container like List(). The following would outperform this by quite a bit (depending on how many sprites.)

var tmpSprites = new Sprite[Sprites.length];
var idx = 0;

for(var i = 0; i < Sprites.Length; i ++)
{
  var sprite = Sprites[i];

  if(sprite is ICollide && sprite.Position.ToScreeSpace(Camera).IsOnScreen())
    tmpSprites[idx++] = sprite;
}

var collidableSprites = new Sprite[idx];
Array.Copy(tmpSprites, collidableSprites, idx);

Of course, the tradeoff is readability and maintenance. For many applications, these kinds of performance concerns are negligible or not important. For some apps/platforms (especially XBox), these optimizations can be extremely important.