Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created February 24, 2023 06:55
Show Gist options
  • Save guitarrapc/0c7e1b21e6bf1f886d28213669ab2981 to your computer and use it in GitHub Desktop.
Save guitarrapc/0c7e1b21e6bf1f886d28213669ab2981 to your computer and use it in GitHub Desktop.
Benchmark.NET with Stopwatch
async Task Main()
{
var summary = BenchmarkRunner.Run<BenchmarkTarget>();
// var sw = ValueStopwatch.StartNew();
// await Task.Delay(1 * 1000);
// sw.GetElapsedTime().Dump();
// await Task.Delay(1 * 1000);
// sw.GetElapsedTime().Dump();
// await Task.Delay(1 * 1000);
// sw.GetElapsedTime().Dump();
//
// var sw2 = ValueStopwatch.StartNew();
// await Task.Delay(1 * 1000);
// sw.GetElapsedTime().Dump("sw");
// sw2.GetElapsedTime().Dump("sw2");
//
// var sw3 = ValueStopwatch.StartNew();
// await Task.Delay(1 * 1000);
// sw.GetElapsedTime().Dump("sw");
// sw2.GetElapsedTime().Dump("sw2");
// sw3.GetElapsedTime().Dump("sw3");
}
[ShortRunJob]
[MemoryDiagnoser]
public class BenchmarkTarget
{
[Benchmark]
public void Stopwatch()
{
for (var i = 0; i < 10000; i++)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
sw.Stop();
}
}
[Benchmark]
public void ValueStopwatch()
{
for (var i = 0; i < 10000; i++)
{
var sw = UserQuery.ValueStopwatch.StartNew();
sw.GetElapsedTime();
}
}
}
public struct ValueStopwatch
{
private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;
private long _startTimestamp;
public bool IsActive => _startTimestamp != 0;
private ValueStopwatch(long startTimestamp)
{
_startTimestamp = startTimestamp;
}
public static ValueStopwatch StartNew() => new ValueStopwatch(Stopwatch.GetTimestamp());
public TimeSpan GetElapsedTime()
{
// Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first thing executed when the machine boots to be 0.
// So it being 0 is a clear indication of default(ValueStopwatch)
if (!IsActive)
{
throw new InvalidOperationException("An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time.");
}
var end = Stopwatch.GetTimestamp();
var timestampDelta = end - _startTimestamp;
var ticks = (long)(TimestampToTicks * timestampDelta);
return new TimeSpan(ticks);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment