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
Posts Tagged forEach
C++ doesn’t have a
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:
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
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!
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
Lists. 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
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 (
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!
Four years ago I tested the functional programming-style methods of
some. In that article I showed that these functions are much slower than doing the same task through traditional loops. Today’s article seeks to improve the performance of the functional methods while retaining readability by using ASC 2.0’s
[Inline] metadata. Can homemade versions of these functions beat the built-in ones from Adobe? Read on to find out!