Skip to content

Instantly share code, notes, and snippets.

@FlaShG
Last active January 3, 2024 15:40
Show Gist options
  • Save FlaShG/8335f3ccedc7a42b92976f54181e9bf4 to your computer and use it in GitHub Desktop.
Save FlaShG/8335f3ccedc7a42b92976f54181e9bf4 to your computer and use it in GitHub Desktop.
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 readonly struct Result
{
private static readonly string[] units = new string[] { "s", "ms", "µs", "ns" };
private readonly long elapsedMilliseconds;
private readonly 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 OneMillion = 1000000;
public const long TenMillion = 10000000;
public const long HundredMillion = 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 = TenMillion)
{
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 = TenMillion)
{
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