Advertisement
earlution

MeasureExecutionTime

Nov 6th, 2023
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         // Create a Stopwatch instance
  9.         Stopwatch stopwatch = new Stopwatch();
  10.  
  11.         // Start measuring time
  12.         stopwatch.Start();
  13.  
  14.         // Call the method you want to measure
  15.         MyMethodToMeasure();
  16.  
  17.         // Stop measuring time
  18.         stopwatch.Stop();
  19.  
  20.         // Get the elapsed time in milliseconds
  21.         long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
  22.  
  23.         Console.WriteLine($"Execution time: {elapsedMilliseconds} ms");
  24.     }
  25.  
  26.     static void MyMethodToMeasure()
  27.     {
  28.         // The method you want to measure
  29.         for (int i = 0; i < 1000000; i++)
  30.         {
  31.             // Do some work
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement