title: Get Average execution time of a method C#
body: <p>This method works fine. But I want to know if there's any other way or make this method more accurate and my ultimate goal is to run the same method 10 times and get the average execution time of it.</p>
  <pre><code>   public static void Measure(Action action, int noOfTimesToRunThisMethod)
     {
          long averageTime = 0;
  
          GC.Collect();  
          GC.WaitForPendingFinalizers();    
              
          Console.WriteLine(&quot;Started&quot;);
  
          for (int i = 1; i &lt;= noOfTimesToRunThisMethod; i++)
          {
             Stopwatch watch = Stopwatch.StartNew();              
             action();
             watch.Stop();
             Console.WriteLine($&quot;Attempt:{i.ToString(&quot;D2&quot;)} Time Taken:{watch.ElapsedMilliseconds} ms&quot;);
             averageTime += watch.ElapsedMilliseconds;
          }
          Console.WriteLine($&quot;Average: {averageTime/ noOfTimesToRunThisMethod}&quot;);
     }
  </code></pre>
  <p>To use this helper method</p>
  <pre><code>   var noOfTimesToRunThisMethod = 10;
             
     Utils.Measure(() =&gt; 
     {  
       MyMethod(arr)
     },noOfTimesToRunThisMethod);
  
  </code></pre>