Every time I see
for (var i = 0; i < array.Length; ++i) I wonder if accessing that
Length property is slow. Should I cache it? It’s comforting to know that
for (int i = 0, len = array.Length; i < len; ++i) is only dealing with local variables except on the first loop. Local variables must be faster, right? Likewise, I wonder the same thing about
List<T>.Count. I finally got around to running a test to see if caching these length properties makes any performance difference. The answers might surprise you!
Posts Tagged list
Every time I see
We’re nearing the end of the series to build a no-garbage replacement for
System.Linq. Today we tackle functions that work on already-sorted ranges and functions that work on ranges that are in heap order. These include common set operations like “union” and “intersection”. Read on to see how to use them and for the updated library that you can use to eliminate your garbage creation!
Back from a brief break, we pick up this week by finishing up the “modifying sequence operations” with some gems like
RandomShuffle and go through the “partitions” category with functions like
IsPartitioned. These are all solid algorithms with a lot of potential uses, so read on to see how to use them with iterators and for the source code that implements them!
Last week’s article introduced the concept of iterators as an alternative to the GC-heavy
IEnumerable. Today’s article expands the iterator library to include a bunch of more functions to make it useful. Think of these like the extension functions in
IndexOf, etc. These have all been tailored to iterators and none of them will create any garbage whatsoever.
In C#, just about everything is an
IEnumerable. Since LINQ syntax,
foreach loops, and the
System.Linq namespace are all designed to work with
IEnumerable, you’ve got lots of tools to use. Unfortunately, the core of
IEnumerable is the
GetEnumerator function which usually creates garbage and eventually causes memory fragmentation and GC framerate spikes. Do we simply stop using all of these nice tools? Normally the answer is “yes”, but today’s article shows you another way.
Last week I presented a problem: how do you iterate over multiple lists of multiple types in the order of some common field? For example, how would you iterate over a list of
Player and a list of
Enemy by both of their
Health fields? In that article I showed two solutions to iterate over two lists in this way. What I didn’t show were any solutions to handle more than two lists. What if you needed to also iterate over a list of
NPC? Today’s article discusses how to tackle this problem and ends up with a handy utility class that you can use for your own types no matter how many lists you have. Read on to see how!
We know that we should reduce the garbage our code produces to lighten the load on Unity’s garbage collector. The trouble is that many of the ways we’re creating garbage are hidden from us. One such way to inadvertently create a lot of garbage is to use a
foreach loop… at least that’s what we’ve been told. Do
foreach loops really create garbage for all types of arrays, lists, dictionaries, and the rest of the collections? Do they create garbage for every loop or just the first one? Today’s article investigates to put these questions to rest. Are you safe using
foreach loops or should you re-write everything to use
for. Read on to find out!