Skip to content

Instantly share code, notes, and snippets.

@r618
Last active October 24, 2015 12:47
Show Gist options
  • Save r618/43bedca9ac8fd9410827 to your computer and use it in GitHub Desktop.
Save r618/43bedca9ac8fd9410827 to your computer and use it in GitHub Desktop.
simple cached vs. non cached transform access measurement in Unity
Results:
========================================================
iPod Touch 4th gen
========================================================
===== seconds avg / frame batches ( * 100 actual frames )
========================================================
Unity 4.6.9
========================================================
non cached =============================================
1) 3.3334 / 150
2) 3.3335 / 140
3) 3.3381 / 160
cached ==================================================
1) 3.33365 / 80
2) 3.3355 / 150
3) 3.3334 / 160
Unity 5.2.2
========================================================
non cached =============================================
1) 3.3385 / 260
2) 3.33338 / 190
3) 3.33376 / 100
cached ==================================================
1) 3.33339 / 220
2) 3.33335 / 220
3) 3.33345 / 100
========================================================
// Profiler from http://wiki.unity3d.com/index.php/Profiler
using UnityEngine;
using System.Collections;
public class Tester : MonoBehaviour
{
Transform transformCached = null;
void Awake()
{
this.transformCached = this.transform;
}
int iteration = 0;
const int iterationMax = 100;
bool profileCachedTransform = false;
void Update ()
{
if ( iteration == 0 )
{
Profile.Reset();
Profile.StartProfile("Update");
}
if ( iteration++ < iterationMax )
{
float dx = Random.Range( -5, 5 );
float dy = Random.Range( -5, 5 );
Vector3 position;
if (!this.profileCachedTransform )
{
this.transform.position = new Vector3( dx, dy, 0 );
this.transform.Translate( new Vector3( dx, dy, 0 ), Space.Self );
position = this.transform.position;
this.transform.position = position;
}
else
{
this.transformCached.position = new Vector3( dx, dy, 0 );
this.transformCached.Translate( new Vector3( dx, dy, 0 ), Space.Self );
position = this.transformCached.position;
this.transformCached.position = position;
}
}
else
{
Profile.EndProfile("Update");
// Profile.PrintResults();
double _avg = Profile.AverageSecondPerCallOfProfile("Update");
if ( _avg > 0 )
this.avgSingle = _avg;
this.avgAll += this.avgSingle;
this.profileCount++;
iteration = 0;
}
}
double avgSingle = 0;
double avgAll = 0;
int profileCount = 0;
void OnGUI()
{
GUILayout.Label( "avg ms/100: " + this.avgSingle.ToString("F9") + " avg ms/all: " + (this.avgAll / this.profileCount).ToString("F9") + " | " + this.profileCount );
if ( GUILayout.Button( this.profileCachedTransform ? "Profiling cached transform - switch" : "Profiling transform - switch", GUILayout.MaxHeight( 40 ) ) )
{
this.avgSingle = this.avgAll = this.profileCount = 0;
this.profileCachedTransform = !this.profileCachedTransform;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment