Skip to content

Instantly share code, notes, and snippets.

@instance-id
Last active July 29, 2020 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save instance-id/1c60d2e4d20a2299e41cc90cdde34051 to your computer and use it in GitHub Desktop.
Save instance-id/1c60d2e4d20a2299e41cc90cdde34051 to your computer and use it in GitHub Desktop.
Results of comparison tests
Test1: Comparison using: go.tag == "value";
Test2: Comparison using: go.CompareTag("Value");
Test3: Comparison using: go.GetComponent<>().Equals(value);
Test4: Comparison using: go.GetComponent<>().field == value;
Test5: Comparison using: ScriptableObject: go.HasTag(tagObject);
[Log] [12:26:02]
Test1 time: 0.041ms (4116.13 ticks) Count: 100000
Test2 time: 0.022ms (2216.14 ticks) Count: 100000
Test3 time: 0.025ms (2581.18 ticks) Count: 100000
Test4 time: 0.025ms (2552.7 ticks) Count: 100000
Test5 time: 0.011ms (1110.6 ticks) Count: 100000
[Log] [12:26:02]
Test1 time: 0.038ms (3899.63 ticks) Count: 100000
Test2 time: 0.023ms (2358.48 ticks) Count: 100000
Test3 time: 0.024ms (2495.23 ticks) Count: 100000
Test4 time: 0.025ms (2510.68 ticks) Count: 100000
Test5 time: 0.011ms (1149.9 ticks) Count: 100000
[Log] [12:26:02]
Test1 time: 0.04ms (4001.37 ticks) Count: 100000
Test2 time: 0.023ms (2399.91 ticks) Count: 100000
Test3 time: 0.025ms (2528.8 ticks) Count: 100000
Test4 time: 0.025ms (2575.85 ticks) Count: 100000
Test5 time: 0.011ms (1178.48 ticks) Count: 100000
[Log] [12:26:03]
Test1 time: 0.038ms (3854.92 ticks) Count: 100000
Test2 time: 0.022ms (2298.34 ticks) Count: 100000
Test3 time: 0.025ms (2506.11 ticks) Count: 100000
Test4 time: 0.025ms (2529.03 ticks) Count: 100000
Test5 time: 0.012ms (1242.61 ticks) Count: 100000
[Log] [12:26:03]
Test1 time: 0.039ms (3937.45 ticks) Count: 100000
Test2 time: 0.023ms (2311.56 ticks) Count: 100000
Test3 time: 0.025ms (2503.31 ticks) Count: 100000
Test4 time: 0.024ms (2463.07 ticks) Count: 100000
Test5 time: 0.011ms (1142.34 ticks) Count: 100000
[Log] [12:26:03]
Test1 time: 0.039ms (3926.93 ticks) Count: 100000
Test2 time: 0.023ms (2359.79 ticks) Count: 100000
Test3 time: 0.025ms (2512.77 ticks) Count: 100000
Test4 time: 0.025ms (2570.87 ticks) Count: 100000
Test5 time: 0.012ms (1207.86 ticks) Count: 100000
[Log] [12:26:03]
Test1 time: 0.038ms (3804.8 ticks) Count: 100000
Test2 time: 0.022ms (2292.1 ticks) Count: 100000
Test3 time: 0.025ms (2566.21 ticks) Count: 100000
Test4 time: 0.026ms (2638.39 ticks) Count: 100000
Test5 time: 0.011ms (1182.4 ticks) Count: 100000
[Log] [12:26:03]
Test1 time: 0.04ms (4085.26 ticks) Count: 100000
Test2 time: 0.023ms (2376.73 ticks) Count: 100000
Test3 time: 0.024ms (2492.07 ticks) Count: 100000
Test4 time: 0.025ms (2594.44 ticks) Count: 100000
Test5 time: 0.012ms (1212.78 ticks) Count: 100000
[Log] [12:26:03]
Test1 time: 0.038ms (3871.27 ticks) Count: 100000
Test2 time: 0.023ms (2300.9 ticks) Count: 100000
Test3 time: 0.024ms (2481.47 ticks) Count: 100000
Test4 time: 0.025ms (2536.15 ticks) Count: 100000
Test5 time: 0.011ms (1194.89 ticks) Count: 100000
[Log] [12:26:03]
Test1 time: 0.037ms (3779.44 ticks) Count: 100000
Test2 time: 0.023ms (2311.07 ticks) Count: 100000
Test3 time: 0.025ms (2531.11 ticks) Count: 100000
Test4 time: 0.025ms (2540.28 ticks) Count: 100000
Test5 time: 0.012ms (1230.25 ticks) Count: 100000
[Button("Run Performance Test")]
public void PerformanceGauge()
{
for (int t = 0; t < 10; t++)
{
// ------------------------------------------------------------------------------ Test1
// -- Waypoint Test1 Comparison using '==' --------------------------------------------
var test1Tagged = new Dictionary<GameObject, bool>();
var test1Count = 0;
var test1Stopwatch = new Stopwatch();
test1Stopwatch.Start();
for (int w = 0; w < 100; w++)
{
for (int i = 0; i < waypointList.Count; i++)
{
test1Tagged.Add(waypointList[i].gameObject, waypointList[i].gameObject.tag == "Waypoint");
test1Count++;
}
test1Tagged.Clear();
}
test1Stopwatch.Stop();
// ------------------------------------------------------------------------------ Test2
// -- Waypoint Test2 Comparison using .CompareTag() -----------------------------------
var test2Tagged = new Dictionary<GameObject, bool>();
var test2Count = 0;
var test2Stopwatch = new Stopwatch();
test2Stopwatch.Start();
for (int w = 0; w < 100; w++)
{
for (int i = 0; i < waypointList.Count; i++)
{
test2Tagged.Add(waypointList[i].gameObject, waypointList[i].gameObject.CompareTag("Waypoint"));
test2Count++;
}
test2Tagged.Clear();
}
test2Stopwatch.Stop();
// ------------------------------------------------------------------------------ Test3
// -- Waypoint Test3 Comparison using GetComponent<>().Equals -------------------------
var test3Tagged = new Dictionary<GameObject, bool>();
var test3Count = 0;
var test3Stopwatch = new Stopwatch();
test3Stopwatch.Start();
for (int w = 0; w < 100; w++)
{
for (int i = 0; i < waypointList.Count; i++)
{
test3Tagged.Add(waypointList[i].gameObject, waypointList[i].gameObject.GetComponent<Waypoint>().waypointID.Equals(i));
test3Count++;
}
test3Tagged.Clear();
}
test3Stopwatch.Stop();
// ------------------------------------------------------------------------------ Test4
// -- Waypoint Test4 Comparison using GetComponent<>().field == -----------------------
var test4Tagged = new Dictionary<GameObject, bool>();
var test4Count = 0;
var test4Stopwatch = new Stopwatch();
test4Stopwatch.Start();
for (int w = 0; w < 100; w++)
{
for (int i = 0; i < waypointList.Count; i++)
{
test4Tagged.Add(waypointList[i].gameObject, waypointList[i].gameObject.GetComponent<Waypoint>().waypointID == i);
test4Count++;
}
test4Tagged.Clear();
}
test4Stopwatch.Stop();
// ------------------------------------------------------------------------------ Test5
// -- Waypoint Test5 Comparison using ScriptableObject Tagging ------------------------
var test5Tagged = new Dictionary<GameObject, bool>();
var test5Count = 0;
var test5Stopwatch = new Stopwatch();
test5Stopwatch.Start();
for (int w = 0; w < 100; w++)
{
for (int i = 0; i < waypointList.Count; i++)
{
test5Tagged.Add(waypointList[i].gameObject, waypointList[i].gameObject.HasTag(TagManager.waypointTag));
test5Count++;
}
test5Tagged.Clear();
}
test5Stopwatch.Stop();
var output =
$"Test1 time: {test1Stopwatch.ElapsedMilliseconds / 1000f}s ({test1Stopwatch.ElapsedTicks / 100f} ticks) Count: {test1Count}\n" +
$"Test2 time: {test2Stopwatch.ElapsedMilliseconds / 1000f}s ({test2Stopwatch.ElapsedTicks / 100f} ticks) Count: {test2Count}\n" +
$"Test3 time: {test3Stopwatch.ElapsedMilliseconds / 1000f}s ({test3Stopwatch.ElapsedTicks / 100f} ticks) Count: {test3Count}\n" +
$"Test4 time: {test4Stopwatch.ElapsedMilliseconds / 1000f}s ({test4Stopwatch.ElapsedTicks / 100f} ticks) Count: {test4Count}\n" +
$"Test5 time: {test5Stopwatch.ElapsedMilliseconds / 1000f}s ({test5Stopwatch.ElapsedTicks / 100f} ticks) Count: {test5Count}\n";
Debug.Log(output);
}
}
@KaleSurfer
Copy link

Awesome benchmark!

change ms to s:
{test1Stopwatch.ElapsedMilliseconds / 1000f}s rather than {test1Stopwatch.ElapsedMilliseconds / 1000f}ms

@instance-id
Copy link
Author

Awesome benchmark!

change ms to s:
{test1Stopwatch.ElapsedMilliseconds / 1000f}s rather than {test1Stopwatch.ElapsedMilliseconds / 1000f}ms

Thanks, I appreciate it. Ah yes, good call. I was originally switching between different time formats to try to see what ended up reading the best and missed that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment