rspec/rules/S6561/csharp/non-compliant-example-benchmark.adoc

20 lines
589 B
Plaintext

==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
var start = DateTime.Now; // First call, on March 26th 2:59 am
MethodToBeBenchmarked();
Console.WriteLine($"{(DateTime.Now - start).TotalMilliseconds} ms"); // Second call happens 2 minutes later but `Now` is March 26th, 4:01 am as there's a shift to summer time
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
var stopWatch = Stopwatch.StartNew(); // Compliant
MethodToBeBenchmarked();
stopWatch.Stop();
Console.WriteLine($"{stopWatch.ElapsedMilliseconds} ms");
----