Thursday, June 11, 2015

Unity3D Micro Optimisation: Loops

This benchmark answers two questions.

1. What is the performance difference between different loop statements? The two options considered are "for" and "foreach". Why bother? These things matter when dealing with modifying 4000 particles per frame in your latest Unity game on a mobile device.

2. What is the performance difference between different collections? Arrays and Generic lists are commonly used in Unity, and I've always wondered about the performance difference of accessing arrays by index, vs lists by index.

The results are quite surprising. Benchmark code below, but here are my results:

  • For Each Loop over Array: 262 ticks
  • For Loop over Array: 120 ticks
  • For Each Loop over List: 1640 ticks
  • For Loop over List: 598 ticks

The (expected) winner is clearly a plain for loop over an array, but I didn't realise the difference between the loop structures and collections would be so large!


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

public class LoopBenchmarks : MonoBehaviour
{
    public int iterations = 15;
    public int arraySize = 40;

    void Start ()
    {
        BenchArray ();
        BenchList ();
    }

    void BenchArray ()
    {
        var array = new int[arraySize];
        var clock = new Stopwatch ();
        var foreachTime = 0f;
        var forTime = 0f;
        var mapTime = 0f;
        for (var iteration=0iteration<iterationsiteration++) {
            clock.Start ();
            foreach (var i in array) {
            }
            clock.Stop ();
            foreachTime += clock.ElapsedTicks;
            clock.Reset ();
            clock.Start ();
            for (var idx=0idx<array.Lengthidx++) {
                var i = array [idx];
            }
            clock.Stop ();
            forTime += clock.ElapsedTicks;
            clock.Reset ();
        }
        UnityEngine.Debug.Log (string.Format ("For Each Loop over Array: {0} ticks"foreachTime / iterations));
        UnityEngine.Debug.Log (string.Format ("For Loop over Array: {0} ticks"forTime / iterations));
    }

    void BenchList ()
    {
        var list = new List<int> (new int[arraySize]);
        var clock = new Stopwatch ();
        var foreachTime = 0f;
        var forTime = 0f;
        for (var iteration=0iteration<iterationsiteration++) {
            clock.Start ();
            foreach (var i in list) {
            }
            clock.Stop ();
            foreachTime += clock.ElapsedTicks;
            clock.Reset ();
            clock.Start ();
            for (var idx=0idx<list.Countidx++) {
                var i = list [idx];
            }
            clock.Stop ();
            forTime += clock.ElapsedTicks;
            clock.Reset ();
        }
        UnityEngine.Debug.Log (string.Format ("For Each Loop over List: {0} ticks"foreachTime / iterations));
        UnityEngine.Debug.Log (string.Format ("For Loop over List: {0} ticks"forTime / iterations));
    }
    
}


Update: Thanks to a comment below, I've added in a new benchmark, and run the test in the latest Unity5.1; The results are surprising. Surprising enough that I added some work to each benchmark just to be sure the compiler wasn't optimising away the useless loops.

  • For Each Loop over Array: 184 ticks
  • For Loop over Array: 206 ticks
  • For Loop over Array with cached Length: 201 ticks
  • For Each Loop over List: 1153 ticks
  • For Loop over List: 686 ticks

It seems that the foreach loop over an array is now FASTER than a for loop. The other surprise is that caching the Length attribute has a very small impact on performance. These results were unexpected... if I get time I'll do some more tests to confirm.

This is the new Benchmark code.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

public class LoopBenchmarks : MonoBehaviour
{
  public int iterations = 1500;
  public int arraySize = 4000;
  
  void Start ()
  {
    BenchArray ();
    BenchArrayWithLengthCached ();
    BenchList ();
  }
  
  void BenchArray ()
  {
    var array = new int[arraySize];
    var clock = new Stopwatch ();
    var foreachTime = 0f;
    var forTime = 0f;
    for (var iteration=0; iteration<iterations; iteration++) {
      clock.Start ();
      foreach (var i in array) {
        if (i < 0)
          throw new System.Exception ();
      }
      clock.Stop ();
      foreachTime += clock.ElapsedTicks;
      clock.Reset ();
      clock.Start ();
      for (var idx=0; idx<array.Length; idx++) {
        var i = array [idx];
        if (i < 0)
          throw new System.Exception ();
      }
      clock.Stop ();
      forTime += clock.ElapsedTicks;
      clock.Reset ();
    }
    UnityEngine.Debug.Log (string.Format ("For Each Loop over Array: {0} ticks", foreachTime / iterations));
    UnityEngine.Debug.Log (string.Format ("For Loop over Array: {0} ticks", forTime / iterations));
  }

  void BenchArrayWithLengthCached ()
  {
    var array = new int[arraySize];
    var clock = new Stopwatch ();

    var forTime = 0f;

    for (var iteration=0; iteration<iterations; iteration++) {
      int length = array.Length;
      clock.Start ();
      for (var idx=0; idx<length; idx++) {
        var i = array [idx];
        if (i < 0)
          throw new System.Exception ();
      }
      clock.Stop ();
      forTime += clock.ElapsedTicks;
      clock.Reset ();
    }
    UnityEngine.Debug.Log (string.Format ("For Loop over Array with Cached Length: {0} ticks", forTime / iterations));
  }
  
  void BenchList ()
  {
    var list = new List<int> (new int[arraySize]);
    var clock = new Stopwatch ();
    var foreachTime = 0f;
    var forTime = 0f;
    for (var iteration=0; iteration<iterations; iteration++) {
      clock.Start ();
      foreach (var i in list) {
        if (i < 0)
          throw new System.Exception ();
      }
      clock.Stop ();
      foreachTime += clock.ElapsedTicks;
      clock.Reset ();
      clock.Start ();
      for (var idx=0; idx<list.Count; idx++) {
        var i = list [idx];
        if (i < 0)
          throw new System.Exception ();
      }
      clock.Stop ();
      forTime += clock.ElapsedTicks;
      clock.Reset ();
    }
    UnityEngine.Debug.Log (string.Format ("For Each Loop over List: {0} ticks", foreachTime / iterations));
    UnityEngine.Debug.Log (string.Format ("For Loop over List: {0} ticks", forTime / iterations));
  }
  
}

2 comments:

Anonymous said...

For iterating over a list, caching the Length parameter in a local variable is a must, and makes a huge difference.

It also is very dependent on what compiler you are using.

Unfortunately for "foreach," C#'s iteration model is extremely heavy with a minimum of one malloc for the loop and two function calls per iteration, so if your compiler cannot inline those function calls you will get significantly worse performance.

Simon Wittber said...

Thanks for the feedback, I'll add that in and post results.

Popular Posts