Unity optimization tips & tricks: Foreach loops, part 2

Foreach loops, part 2 This is part 2 out of a 2-part series on foreach loops. Click here for part 1. Last time, we played around with foreach loops, and ended up deconstructing the loop into an iterative while loop. For fun and for profit- and for superior performance in the Unity engine! Today though, we’re going a little deeper. But first, let’s take a look a the code we ended with last week and see where we can improve: int iterationCount = 0; while(iterationCount < collection.Length) { var element = collection[iterationCount]; // the body of the foreach loop iterationCount ++; } First things first – I’m going to cache the length of the array (or the count of a Read more about Unity optimization tips & tricks: Foreach loops, part 2[…]

Unity optimization tips & tricks: Foreach, the Vampire loop

Foreach, the Vampire loop This is part 1 out of a 2-part series on foreach loops. Click here for part 2. What makes code tick? Make code run fast often seems like mysterious, voodoo-like dark magic. But in reality, by using simple tools and our brain we can get you quite far. So today, we’re gonna cover the surprising amount of impact your foreach loops can have on code execution speed. Under the hood Picture this: you’re working on a large array of AI data for your latest game, but the performance just isn’t there, no matter how simple you try to keep the functions. So you start profiling your code to see what’s wrong. As you go deeper and Read more about Unity optimization tips & tricks: Foreach, the Vampire loop[…]