Skip to content
</> Mücahit TopalA developer's notebook

Blog/C#

Measuring Code Execution Time with C#

Mücahit Topal 1 min read 5,185 views TREN

When we need to find out how long the code we wrote takes to run, we can use the Stopwatch object in .NET.

 Let's add the required namespace:
 

using System.Diagnostics;

And our code looks like this:

Stopwatch izle  = new Stopwatch();

izle.Start();
if (conn.State == ConnectionState.Closed)
{
    conn.Open();
}
izle.Stop();

MessageBox.Show("Connection time: {0}", izle.Elapsed.Milliseconds);

 

The measurement is started with Start and ended with Stop, and the elapsed time is held in the Elapsed variable. You can get it as Hours, Minutes, Seconds or Miliseconds.

,,,,

Share X Facebook LinkedIn WhatsApp

More from this category

Comments 0

No comments yet.

Leave a comment

Comments appear after approval.