Benchmark.NET with Stopwatch
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
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