Skip to content

Instantly share code, notes, and snippets.

@FlaShG
Last active September 7, 2021 07:46
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
A C# Benchmark class.
using System;
using System.Diagnostics;
/// <summary>
/// Simple benchmark class to measure average time consumption of code.
/// </summary>
public static class Benchmark
{
/// <summary>
/// A benchmark's time result.
/// Use ToString() to display it.
/// </summary>
public struct Result
{
private static readonly string[] units = new string[] { "s", "ms", "µs", "ns" };
private long elapsedMilliseconds;
private long iterationCount;
public float elapsedSeconds
{
get { return ((float)elapsedMilliseconds) / (iterationCount * 1000); }
}
public Result(long elapsedMilliseconds, long iterationCount)
{
this.elapsedMilliseconds = elapsedMilliseconds;
this.iterationCount = iterationCount;
}
public float CompareTo(Result other)
{
var elapsed = elapsedSeconds;
var otherElapsed = other.elapsedSeconds;
return elapsed / otherElapsed;
}
public override string ToString()
{
var elapsedTime = elapsedSeconds;
var order = 0;
while(elapsedTime < 1 && order < units.Length - 1)
{
elapsedTime *= 1000;
order++;
}
return elapsedTime + units[order];
}
}
public const long ONE_MILLION = 1000000;
public const long TEN_MILLION = 10000000;
public const long HUNDRED_MILLION = 100000000;
/// <summary>
/// Returns the time it took on average to perform action.
/// </summary>
/// <param name="action">The action to benchmark.</param>
/// <param name="iterationCount">The amount of times to perform the action.</param>
/// <returns>The time it took on average to perform action.</returns>
public static Result Run(Action action, long iterationCount = TEN_MILLION)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
for(var l = 0L; l < iterationCount; l++)
{
action();
}
stopwatch.Stop();
return new Result(stopwatch.ElapsedMilliseconds, iterationCount);
}
/// <summary>
/// Returns the time it took on average to perform action. Allows an action to be performed each time without being measured.
/// </summary>
/// <param name="action">The action to benchmark.</param>
/// <param name="actionBefore">The action to be performed before each cycle without being measured.</param>
/// <param name="iterationCount">The amount of times to perform the action.</param>
/// <returns>The time it took on average to perform action.</returns>
public static Result Run(Action action, Action actionBefore, long iterationCount = TEN_MILLION)
{
var stopwatch = new Stopwatch();
for (var l = 0L; l < iterationCount; l++)
{
actionBefore();
stopwatch.Start();
action();
stopwatch.Stop();
}
return new Result(stopwatch.ElapsedMilliseconds, iterationCount);
}
}
using UnityEngine;
public class BenchmarkExample : MonoBehaviour
{
private void Start()
{
BenchmarkResult result;
result = Benchmark.Run(() => Something.SomeStuff());
Debug.Log("SomeStuff takes " + result);
result = Benchmark.Run(() => Something.OtherStuff());
Debug.Log("OtherStuff takes " + result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment