---
title: "How to calculate the code execution time in C#?"  
description: "How to calculate the code execution time in C#?"  
author: "Sandra Emily"  
published: 2024-06-13  
updated: 2024-06-13  
canonical: https://www.mindstick.com/forum/160738/how-to-calculate-the-code-execution-time-in-c-sharp  
category: "c#"  
tags: ["c#", "programming language", "code optimization"]  
reading_time: 2 minutes  

---

# How to calculate the code execution time in C#?

How to calculate the [code execution](https://www.mindstick.com/forum/160098/explain-the-difference-between-synchronous-and-asynchronous-code-execution-in-javascript) time in C#?

## Replies

### Reply by Ashutosh Patel

### Calculate the Code Execution Time

You can calculate the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) time of a piece of [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) in C# using the Stopwatch class from the `System.Diagnostics` namespace.

## Example-

Here is a basic example to calculate the code execution time in c#,

```cs
using System;
using System.Diagnostics;
class Program
{
   static void Main()
   {
       // Create a Stopwatch instance
       Stopwatch stopwatch = new Stopwatch();
       // Start measuring time
       stopwatch.Start();
       // Operate whose execution time you want to measure
       // For example, a loop that runs for some time
       for (int i = 0; i < 5; i++)
       {
           // Print a message 5 time to increase the execution time
           Console.WriteLine("Print text to take some execution time");
       }
       // Stop measuring time
       stopwatch.Stop();
       // Get the elapsed time
       TimeSpan elapsedTime = stopwatch.Elapsed;
       // Print the elapsed time in milliseconds
       Console.WriteLine("\nExecution Time: " + elapsedTime.TotalMilliseconds + " ms");
       Console.ReadLine();
   }
}
```

## In the above example-

We create an instance of `Stopwatch`named **stopwatch**.

We **start** the stopwatch using the `Start()` method.

Let’s create a task whose execution time we want to measure. This can be any code block or method.

We stop the stopwatch with the `Stop()` method.

We retrieve the elapsed time using the `Elapsed`property, which returns a `TimeSpan`object representing the elapsed time.

Finally, we print the **elapsed time** to the console.

## Output-

```plaintext
Print text to take some execution time
Print text to take some execution time
Print text to take some execution time
Print text to take some execution time
Print text to take some execution time

Execution Time: 0.7662 ms
```

Be sure to add the System.Diagnostics you will use; At the top of your file to use the Stopwatch class. This will provide an accurate measurement of the execution time of a specified rule.

**Also, Read:** [How to read file using StreamReader in C#?](https://www.mindstick.com/forum/160739/how-to-read-file-using-streamreader-in-c-sharp)


---

Original Source: https://www.mindstick.com/forum/160738/how-to-calculate-the-code-execution-time-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
