Skip to content

Instantly share code, notes, and snippets.

@joncloud
Created August 7, 2017 14:36
Show Gist options
  • Select an option

  • Save joncloud/f217d8946cd94ff0ccb4e33f790247b9 to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/f217d8946cd94ff0ccb4e33f790247b9 to your computer and use it in GitHub Desktop.
Stopwatch Performance
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Diagnostics;
namespace ConsoleApp37
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Watcher>();
}
}
[MemoryDiagnoser]
public class Watcher
{
private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;
[Benchmark]
public TimeSpan Ctor()
{
Stopwatch watch = new Stopwatch();
watch.Start();
watch.Stop();
return watch.Elapsed;
}
[Benchmark]
public TimeSpan Calc()
{
long start = Stopwatch.GetTimestamp();
var now = Stopwatch.GetTimestamp();
return new TimeSpan((long)(TimestampToTicks * (now - start)));
}
}
}
BenchmarkDotNet=v0.10.9, OS=Windows 10 Redstone 1 (10.0.14393)
Processor=Intel Core i7-4870HQ CPU 2.50GHz (Haswell), ProcessorCount=8
Frequency=2435764 Hz, Resolution=410.5488 ns, Timer=TSC
.NET Core SDK=2.0.0-preview2-006497
  [Host]     : .NET Core 1.1.2 (Framework 4.6.25211.01), 64bit RyuJIT
  DefaultJob : .NET Core 1.1.2 (Framework 4.6.25211.01), 64bit RyuJIT

Method Mean Error StdDev Gen 0 Allocated
Ctor 43.05 ns 0.2424 ns 0.2149 ns 0.0063 40 B
Calc 33.16 ns 0.6466 ns 0.6048 ns - 0 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment