Go Back

C# Linq2Objects Performance

A new feature in C# 3.0 is Linq allowing wonderful queries like this:

var entity = _entities.FirstOrDefault(e => e.Id == id);

Its so easy to use these type of queries that you quickly forget about possible performance issues associated with queries like this. Basically a query like this will do a foreach-loop for all the objects present in _entities. No look up table of any kind is used.

In a private bot I am working on, I was reading game-entities (players, npcs, etc) from memory. I have to run through 2 lists in memory (one list contains entity positions, the other contains entity names). Reading both lists required me to see if an entity with an Id existed and if so, update its name / position.

Now this was taking 5ms for around 400-500 entities. Not a huge amount of time, but since this bot reads the entities every frame, it added up for some nice lag. I tried some different solutions, but the most effective way to solve this lag was by replacing the _entity.FirstOrDefault(e => e.Id == id) / _entity.Any(e => e.Id == id) with a Dictionary<int,Entity> object and then using its ContainsKey as a replacement for Any.

The difference was huge, the process time went down to 0ms (according to C#'s StopWatch class) for the same amount of entities.

So a quick note for people using C#'s L2O, if your looking up an Id field in a list of objects, its most likely better to use a separate hashtable (Dictionary) object.

Posted by: Da_Teach on Sunday, January 10, 2010  •  Comments (10)  •  Full story  •  C# Linq