title: Correctly measure time duration in Go
body: <p>What is the correct way to precisely measure a time duration in Go?
  Most application just use the standard time package and the following approach:</p>
  
  <pre><code>var startTime = time.Now()
  doSomeHardWork()
  var duration = time.Since(startTime) // or: time.Now() - startTime
  </code></pre>
  
  <p>However, <code>time.Now()</code> returns the current system time, which leads to two flaws:</p>
  
  <ol>
  <li>If the system time is changed during the measurement (for example due to a <strong>time zone change</strong> (DST) or a <strong>leap second</strong>), the resulting duration is also wrong.</li>
  <li><p>The system time can tick deliberately faster or slower than the real time. This always happens when the operating system synchronizes the internal clock with NTP time servers (which might happen several times an hour!)</p>
  
  <p>From <a href="https://technet.microsoft.com/en-us/library/cc773013(v=ws.10).aspx" rel="noreferrer">MSDN</a>: </p>
  
  <blockquote>
    <p>[The time service] adjusts the local clock rate to allow it to
    converge toward the correct time. If the time difference between the
    local clock and the [accurate time sample] is too large to correct by
    adjusting the local clock rate, the time service sets the local clock
    to the correct time.</p>
  </blockquote></li>
  </ol>
  
  <p>If the system time changes (either manually or due to DST), it might be possible to detect the invalid duration and discard it. But if the system clock ticks e.g. 10% faster to synchronize with world-time, it is practically impossible to detect. That's intended behaviour and how the system clock is designed.</p>
  
  <p>For that reason, most other languages offer a dedicated API for measuring durations:</p>
  
  <ul>
  <li>Java has <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#nanoTime%28%29" rel="noreferrer"><code>System.nanoTime()</code></a>, <code>System.currentTimeMillis()</code> would be equivalent to <code>time.Now()</code> and is wrong</li>
  <li>C# has <a href="https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx" rel="noreferrer"><code>System.Diagnostics.Stopwatch</code></a></li>
  <li>C/C++ on Windows has <a href="https://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx" rel="noreferrer"><code>QueryPerformanceCounter</code></a> and <a href="https://msdn.microsoft.com/en-us/library/ms644905(VS.85).aspx" rel="noreferrer"><code>QueryPerformanceFrequency</code></a></li>
  <li>C++11 and up have <a href="http://en.cppreference.com/w/cpp/chrono/steady_clock" rel="noreferrer"><code>std::chrono::steady_clock</code></a>, or <a href="http://en.cppreference.com/w/cpp/chrono/high_resolution_clock" rel="noreferrer"><code>std::chrono::high_resolution_clock</code></a> when its <code>is_steady</code> member constant is <code>true</code></li>
  <li>JavaScript has <a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance/now" rel="noreferrer"><code>performance.now()</code></a>, while the use of <code>new Date()</code> is wrong</li>
  </ul>
  
  <p>What is the correct way to precisely measure execution time in Go?</p>