title: How to measure CPU cycles of a given process in C#?
body: <p>I have some different models that perform the same task, using different algorithms. I want to compare the performance of these models by measuring CPU cycles (I tried using <code>System.Diagnostics.StopWatch</code> to count Ticks, but the result is not accurate enough).</p>
  
  <p>I have found a <a href="https://gist.github.com/zhangz/6094657" rel="nofollow noreferrer">class</a> that measures CPU cycles by using <code>P/Invoke</code>, as follows:</p>
  
  <pre><code>IModel model;
  CodeTimer.Time(true, model.ToString(), totalTime, model.TimeStep);
  </code></pre>
  
  <p>The method above iterates by <code>N</code> times the method <code>model.TimeStep</code>. I noticed that the results of <code>CodeTimer.Time</code> vary greatly (by at least one order of magnitude, from <code>58 KCycles</code> to <code>890 KCycles</code>). Thus, correct me if I am wrong, but as <code>CodeTimer</code> class marks the cycles before and after the process, it can also count the cycles used by any other processes (even OS processes) occurring during the execution of <code>model.TimeStep</code> method (the method which I want to measure performance).</p>
  
  <p>So two questions:</p>
  
  <ol>
  <li>Is my above supposition correct? If it is, then I need to come up with another solution to measure CPU cycles, which leads to my main question:</li>
  </ol>
  
  <p>I then came up with the idea of iterating <code>model.TimeStep()</code> in a different <code>System.Threading.Thread</code> and measuring the cycles of the given thread using <a href="http://msdn.microsoft.com/en-us/library/ms684929.aspx" rel="nofollow noreferrer"><code>QueryProcessCycleTime</code></a>. However as <code>QueryProcessCycleTime</code> receives as input a <code>System.Threading.WaitHandle</code> (or an <code>IntPtr</code> as well), I don't know how to tell this method that I want to measure the cycles of that specific <code>Thread</code> I just created. In another words, I don't know how to put together the <code>thread</code> which I create to iterate the model, the <code>WaitHandle</code> and the <code>QueryProcessCycleTime</code>, even though I have read some <a href="http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml" rel="nofollow noreferrer">examples</a> of using <code>WaitHandle</code> (I just can't mix everything together).</p>
  
  <ol start="2">
  <li>How can I accomplish that?</li>
  </ol>
  
  <p>It would be something like this:</p>
  
  <pre><code>class ModelSimulator
  {
      public Model1 model1 { get; private set; } // implements IModel
      public Model2 model2 { get; private set; } // implements IModel
  
  /* other methods */
  
      public void RunModel(object obj)
      {
          IModel model = (IModel)obj;
          Int32 t = model.maxTime;
          while (t-- &gt; 0)
              model.TimeStep();
      }
  }
  </code></pre>
  
  <p>And inside the <code>main</code> method, I do:</p>
  
  <pre><code>[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern Boolean QueryProcessCycleTime(SafeWaitHandle processHandle, out UInt64 CycleTime);
  
  static void Main(string[] args)
  {
      ModelSimulator ms = new ModelSimulator();
      Thread thread = new Thread(ms.RunModel);
      thread.Start(ms.model1);
      // here I start the thread, but how can I use QueryProcessCycleTime to measure
      // the thread execution
  }
  </code></pre>
  
  <p>PS: mine is not a duplicate of <a href="https://stackoverflow.com/questions/6526828/how-to-get-the-number-of-cpu-cycles-used-by-a-process">this question</a>, because I need some more specific answer, which is not there... My question could be a complement to that question.</p>