User Tools

Site Tools


snippets:time_measurement

Assembly (reference)

System

Using directive

using System.Diagostics;

Code

Stopwatch watch = new Stopwatch();
// ...
watch.Start();
// ... execute the first task to be timed
watch.Stop();
 
watch.Restart(); // reset and starts again (.NET 4.0)
// ... execute the next task to be timed
watch.Stop();
 
// results
Console.WriteLine("Elapsed: {0}",         watch.Elapsed);
Console.WriteLine("In milliseconds: {0}", watch.ElapsedMilliseconds);
Console.WriteLine("In timer ticks: {0}",  watch.ElapsedTicks);

Note

Stopwatch watch = new Stopwatch();
watch.Start();

is equivalent to

Stopwatch watch = Stopwatch.StartNew();
snippets/time_measurement.txt · Last modified: 2015/07/26 10:13 by jaap.dehaan