How do I calculate execution time of a process?
Category: System.Diagnostics, viewed: 387 time(s).
The following example demonstrates how to use the System.Diagnostics.Stopwatch class to calculate execution time of a process in application.
using System;
using System.Diagnostics;
using System.Threading;
namespace Kodecsharp.Example.System.Diagnostics
{
class StopWatchDemo
{
[STAThread]
public static void Main(string[] args)
{
//
// Create a new instance of Stopwatch and call the
// Start method to begin the stop watch counter.
//
Stopwatch sw = new Stopwatch();
Console.WriteLine("Process Start...");
sw.Start();
//
// Simulating some long process here!
//
Thread.Sleep(11000);
//
// Stop the Stopwatch.
//
sw.Stop();
Console.WriteLine("Process Finish...");
//
// Get the elapsed time of the Stopwatch instance.
//
TimeSpan ts = sw.Elapsed;
//
// Display the time recorded by the Stopwatch instance.
//
string elapsedTime = string.Format(
"The process took: {0:00h}:{1:00m}:{2:00s}.{3:0000ms}",
ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
Console.WriteLine(elapsedTime);
Console.ReadLine();
}
}
}