There are many permutations of loops we can write, but what do they compile to? We should know the consequences of using an array versus a List<T>
, for
versus foreach
, caching Length
, and other factors. So today’s article dives into the C++ code that IL2CPP outputs when we write these various types of loops to examine the differences. We’ll even go further and look at the ARM assembly that the C++ compiles to and really find out how much overhead our choices are costing us.
Posts Tagged forEach
C++ doesn’t have a foreach
keyword, but it does have an equivalent in “range for
loops”. Today we’ll implement support for them so we can easily loop over arrays and types implementing IEnumerable
and IEnumerable<T>
.
Over a year ago I wrote an article title Do Foreach Loops Create Garbage using Unity 5.2 and tested foreach
with a variety of collections: List
, Dictionary
, arrays, etc. Since then Unity has a new C# compiler and version 5.6 has been released. Is it safe to use foreach
now? Read on to find out!
Today’s article takes a break from the iterator series to investigate an interesting anomaly with the List.ForEach
function: it’s surprisingly fast! So fast that it’s actually competitive with regular old for
, foreach
, and while
functions. How can it be so fast when it has to call a delegate that you pass it for every single loop iteration? Read on for to find out!
Today’s article expands on the previous loop test article to find out which kind of loop is truly fastest. Read on to find out!
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!
The first version of SafeList
tried to address a common problem: inserting and removing elements into a List<T>
while you loop over it. It had a lot of problems though and ended up being pretty much useless. Today’s article presents SafeList
2.0, a radically-improved version that really solves the problem so you can actually use it as a drop-in replacement for List<T>
. Read on for the details, the source code, and even the unit tests that prove it handles all the nasty corner cases for you!
foreach
loops are really convenient, but are for
loops faster? It’s a simple question, but one that has really wide implications in almost any codebase. Today’s article tests them out to see which is faster for looping over arrays and List
s. Read on to see which is quicker!
Today’s article shows a class that helps clean up your foreach
loops when you want to call Add()
or Remove()
on the List
you’re looping over. Normally you’d get an exception, but today’s class works around that problem so your code is less error-prone and easier to read. It also discusses some workarounds you can use even if you don’t use SafeList
. Read on to learn how to make your foreach
loops less error-prone! UPDATE: SafeList 2.0 is out!
SQL-style LINQ queries are a concise, readable way of performing various tasks dealing with all kinds of collections. Surely all that convenience comes with a performance cost to it. How bad do you think it is? Today we’ll look at the cost of some basic LINQ queries (Where
, Select
) versus the equivalent non-LINQ code. We’ll also see how much slower both of them are compared to manually-written, traditional code that does away with all the flexibility. Read on to see the results!