Created
August 24, 2012 15:18
-
-
Save MattRix/3451913 to your computer and use it in GitHub Desktop.
A tool for comparing the performance of different code in C#. Just call PerfTest.Run();
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
public class Thing | |
{ | |
public float a; | |
public float b; | |
public float c; | |
private float _x; | |
private float _y; | |
private float _z; | |
public float x | |
{ | |
get { return _x;} | |
set {_x = value;} | |
} | |
public float y | |
{ | |
get { return _y;} | |
set {_y = value;} | |
} | |
public float z | |
{ | |
get { return _z;} | |
set {_z = value;} | |
} | |
public Thing() | |
{ | |
} | |
public void Set(float x, float y, float z) | |
{ | |
_x = x; | |
_y = y; | |
_z = z; | |
} | |
} | |
public class PerfTest | |
{ | |
static public int runIterations = 10; | |
static public int testIterations = 5000; | |
static public int loopIterations = 1000; | |
static public List<Thing> things; | |
static public Stopwatch watch = new Stopwatch(); | |
static public void Run() | |
{ | |
float efficiency = 0; | |
for(int a = 0; a<runIterations; a++) | |
{ | |
efficiency += ActuallyRun(a); | |
} | |
efficiency /= runIterations; | |
if(Math.Round (efficiency*33.0) == 33.0) //within 3 percent | |
{ | |
UnityEngine.Debug.Log("All tests complete, they're equal!"); | |
} | |
else if(efficiency < 1.0) | |
{ | |
int percent = (int) Math.Round(1.0/efficiency * 100.0) - 100; | |
UnityEngine.Debug.Log("All tests complete, A is "+percent+"% faster!"); | |
} | |
else | |
{ | |
int percent = (int) Math.Round(efficiency * 100.0) - 100; | |
UnityEngine.Debug.Log("All tests complete, B is "+percent+"% faster!"); | |
} | |
} | |
static private float ActuallyRun(int testIndex) | |
{ | |
long timeA = 0; | |
long timeB = 0; | |
things = new List<Thing>(loopIterations); | |
for(int i = 0; i<loopIterations; i++) | |
{ | |
things.Add(new Thing()); | |
} | |
for(int t = 0; t<testIterations; t++) | |
{ | |
timeA += DoTestA(); | |
timeB += DoTestB(); | |
} | |
//reverse order | |
for(int t = 0; t<testIterations; t++) | |
{ | |
timeB += DoTestB(); | |
timeA += DoTestA(); | |
} | |
float delta = (float)timeA/(float)timeB; | |
UnityEngine.Debug.Log("Test " + testIndex + " A:" + timeA + " B:" + timeB + " efficiency: " + delta); | |
return delta; | |
} | |
public static long DoTestA () | |
{ | |
watch.Reset(); | |
watch.Start(); | |
int count = things.Count; | |
for(int i = 0; i<count; i++) | |
{ | |
Thing thing = things[i]; | |
thing.a = thing.a * 10.0f; | |
} | |
return watch.ElapsedTicks; | |
} | |
public static long DoTestB () | |
{ | |
watch.Reset(); | |
watch.Start(); | |
int count = things.Count; | |
for(int i = 0; i<count; i++) | |
{ | |
Thing thing = things[i]; | |
thing.a *= 10.0f; | |
} | |
return watch.ElapsedTicks; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment