====== Execution time ====== ===== DateTime.UtcNow ===== * very fast * bad resolution (10 milliseconds) DateTime begin = DateTime.UtcNow; ... DateTime end = DateTime.UtcNow; Console.WriteLine("Execution time: " + (end-begin).TotalMilliseconds + " ms."); ===== Stopwatch ===== * little bit slower * can be unreliable on a PC with multiple processors or on processors with unconstant clock speed! Stopwatch watch = new Stopwatch(); watch.Start(); ... watch.Stop(); Console.WriteLine("Execution time: " + watch.Elapsed.TotalMilliseconds + " ms."); ===== Process.TotalProcessorTime ===== * measures only processors time kept by process * if process is keeping multiple processors busy, the time of each processor will be added TimeSpan begin = Process.GetCurrentProcess().TotalProcessorTime; ... TimeSpan end = Process.GetCurrentProcess().TotalProcessorTime; Console.WriteLine("Execution time: " + (end - begin).TotalMilliseconds + " ms."); ===== References ===== * [[http://kristofverbiest.blogspot.com/2008/10/beware-of-stopwatch.html]]