Effect of caching Transform

From Unify Community Wiki
Jump to: navigation, search

This code demonstrates that accessing Transform directly takes about twice as long as accessing a cached version.

System.Diagnostics.Stopwatch s = new Stopwatch();
 
double d = 0;
 
s.Start();
for( long l = 0; l < 10000000; l++ )
    d += transform.position.x;
s.Stop();
 
Debug.Log( s.ElapsedMilliseconds + " (" + d + ")");
 
d = 0;
Transform t = transform;
 
s.Reset();
s.Start();
for( long l = 0; l < 10000000; l++ )
    d += t.position.x;
s.Stop();
 
Debug.Log( s.ElapsedMilliseconds + " (" + d + ")" );
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox