Tuesday, May 29, 2012

Monday, May 28, 2012

Kroolz

As recently promised, "Krool Joolz" has made a comeback on the iOS platform as "Kroolz". Get it here, it's Free!

Monday, May 07, 2012

Micro Optimisation in Unity3D

NGUI caches the transform component in a member variable called mTrans, to optimise speed of access. I think this is UGLY, so I decided to do some benchmarks to see if the ugliness is worth the performance increase.

In short, the answer is NO.

My tests showed that using a local variable is always as fast as using a member variable, and usually faster.

Here are some typical results.

Cached: Time elapsed: 00:00:04.8086550
Uncached: Time elapsed: 00:00:09.1058490
Local: Time elapsed: 00:00:04.7870590

They show that caching and accessing the target reference is twice as fast as just using the reference. They also show that assigning to a local variable and using that variable for access is usually even faster than a member variable.

This is the code I used for benchmarking.

using UnityEngine;
using System.Collections;

public class Benchmark : MonoBehaviour
{
    Transform cachedTransform;
    
    void Start () {
        cachedTransform = transform;
        TestCached();    
        TestUncached();
        TestLocal();
    }
    
    void TestUncached()
    {
        var stopwatch = new System.Diagnostics.Stopwatch ();
        stopwatch.Start ();
        for (int i = 0; i < 100000000; i++) {
            var p = transform.position;
        }
        stopwatch.Stop ();
        Debug.Log (string.Format("Uncached: Time elapsed: {0}", stopwatch.Elapsed));
    }
    
    void TestCached()
    {
        var stopwatch = new System.Diagnostics.Stopwatch ();
        stopwatch.Start ();
        for (int i = 0; i < 100000000; i++) {
            var p = cachedTransform.position;
        }
        stopwatch.Stop ();
        Debug.Log (string.Format("Cached: Time elapsed: {0}", stopwatch.Elapsed));
    }
    
    void TestLocal()
    {
        var stopwatch = new System.Diagnostics.Stopwatch ();
        stopwatch.Start ();
        var t = transform;
        for (int i = 0; i < 100000000; i++) {
            var p = t.position;
        }
        stopwatch.Stop ();
        Debug.Log (string.Format("Local: Time elapsed: {0}", stopwatch.Elapsed));
    }
}

Saturday, May 05, 2012

Rendering HTML

Rendering HTML in Unity3D sure is... tricky.
It would seem simple enough, but page/text layout algorithms can certainly be challenging, even when supporting only a subset of HTML.

Popular Posts