Exact Time Measurement for Performance Testing

This post shows how to exactly measure performance for some code to execute. This is quite useful while doing performance testing. Here’s how it can be done.

C-Sharp

Stopwatch sw = new Stopwatch();

sw.Start();

// ...

sw.Stop();

Console.WriteLine("Elapsed={0}",sw.Elapsed);

Javascript

var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")

This content has been helpful to you?

Thanks for contributing!

Yes No