title: High Resolution Timing Part of Your Code
body: <p>I want to measure the speed of a function within a loop. But why my way of doing it always print "0" instead of high-res timing with 9 digits decimal precision (i.e. in nano/micro seconds)?</p>
  
  <p>What's the correct way to do it?</p>
  
  <pre><code>#include &lt;iomanip&gt;
  #include &lt;iostream&gt;
  #include &lt;time.h&gt;
  int main() {
  
  
   for (int i = 0; i &lt;100; i++) {
      std::clock_t startTime = std::clock(); 
      // a very fast function in the middle
      cout &lt;&lt; "Time: " &lt;&lt; setprecision(9) &lt;&lt; (clock() - startTime + 0.00)/CLOCKS_PER_SEC &lt;&lt; endl;
   }
  
   return 0;
  }
  </code></pre>
  
  <h3>Related Questions:</h3>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/588307/">How to overcome <code>clock()</code>'s low resolution</a></li>
  <li><a href="https://stackoverflow.com/questions/538609/">High Resolution Timer with C++ and linux</a></li>
  <li><a href="https://stackoverflow.com/questions/464618">Equivalent of Windows’ <code>QueryPerformanceCounter</code> on OSX</a></li>
  </ul>