Saturday, July 18, 2015

AsyncCoroutines - Rather Easy Multithreading for Unity

This is a tiny little library to help you use your multicore CPU in Unity3D.
I've made a brief video overview and the package will be available on the asset store very soon.
It includes a task viewer, which is similar to the process viewer on your operating system. It shows which coroutines are running in the background and foreground, which coroutines are waiting for execution, and how many CPU ticks each coroutine consumes per frame.


To use, make sure you have:
using AsyncCoroutines;
at the top of your script file. Below is an example of a coroutine method which can be started using:
gameObject.StartTask(MyCoroutine());
IEnumerator MyCoroutine() {
    //this statement will move the execution to the background thread
    //pool.
    yield return Schedule.GotoBackground;
    //Do some work here
        
    //This statement will move the execution back into the main unity
    //foreground thread.
    yield return Schedule.GotoForeground;

    //When the coroutine is running in the main thread, yield statements
    //will work as normal.
    yield return null;
    yield return WaitForSeconds(1);
}

Tuesday, July 14, 2015

3D Conference in Perth on 22/07/15

An interesting conference for 3D tech next week is happening in Perth. It's only short, 7:30AM - 2PM, however there is some interesting stuff happening. A realtime 3D scan of a Ducati, 3D printing and Oculus VR... I'm 90% sure I'll be there.

More info and registration here.

Tuesday, July 07, 2015

Behaviour and Decision Library for Unity

uBAD is a library for programmers to create AI based on behaviour tree systems. Instead of using a graphical editor to define the tree, it uses a text file with a simple syntax. It includes a visual debugger.

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));
  }
  
}

Popular Posts