How to use Stopwatch Calculate Program Execution time in C#

C Sharp - C# (programming language)
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

How to use Stopwatch Calculate Program Execution time in C#

Post by dzcadii » Sun Mar 01, 2015 10:53 pm

Here is a little snippet of code if you need to calculate program execution time in C#.

Code: Select all

//Use these namespaces
using System;
using System.Diagnostics;
using System.Threading;

//Place this where you want to start the stopwatch
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

//Place this where you want to stop
stopwatch.Stop();
TimeSpan ts = stopwatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

//This is how you would display the results in a console application
Console.WriteLine("All operations have completed in " + elapsedTime + "!\r\nPress Enter to exit!");
Console.ReadLine();

Post Reply