title: Measure CPU cycles of a Function Call
body: <p>I'm looking for a way to measure the cpu cycles a function call on a thread takes.</p>
  
  <p>Example pseudo code:</p>
  
  <pre><code>void HostFunction()
  {
       var startTick = CurrentThread.CurrentTick;  //does not exist
  
       ChildFunction();
  
       var endTick = CurrentThread.CurrentTick;  //does not exist
  
       var childFunctionCost = endTick - startTick;
  }
  
  void ChildFunction() 
  {
      //Do whatever...
  
      Thread.Sleep(3000);
  
      //Do some more...
  }
  </code></pre>
  
  <p>I don't want to use a <strong>Stopwatch</strong> or some other time measurement, because it would include any time that the thread is sleeping, which I do not want to measure.  I only want to measure real work.</p>
  
  <p>This measurement needs to work at runtime, as in my pseudo code, as the results are used to determine if the child function should be allowed to continue to run (my real case is a plugin-type architecture), so a profiling tool won't help me.</p>