title: How to measure the time that a piece of code takes to execute?
body: <p>Suppose I want to measure the time that a certain piece of code takes. For that I would normally do something like this</p>
  
  <pre><code>clock_t startTime = clock();
  //do stuff
  //do stuff
  //do stuff
  //do stuff
  float secsElapsed = (float)(clock() - startTime)/CLOCKS_PER_SEC;
  </code></pre>
  
  <p>What if the program is multithreaded and context switches occur within the part which I want to measure? How would I measure the time that my code takes to execute excluding time spent on other threads? Even if there are tools that do it, I would very much like to know <em>how</em> they're doing it. </p>