title: Measuring elapsed time in python
body: <p>Is there a simple way / module to <em>correctly</em> measure the elapsed time in python? I know that I can simply call <code>time.time()</code> twice and take the difference, but that will yield wrong results if the system time is changed. Granted, that doesn't happen very often, but it does indicate that I'm measuring the wrong thing.</p>
  
  <p>Using <code>time.time()</code> to measure durations is incredibly roundabout when you think about it. You take the difference of two absolute time measurements which are in turn constructed from duration measurements (performed by timers) and known absolute times (set manually or via ntp), that you aren't interested in at all.</p>
  
  <p>So, is there a way to query this "timer time" directly? I'd imagine that it can be represented as a millisecond or microsecond value that has no meaningful absolute representation (and thus doesn't need to be adjusted with system time). Looking around a bit it seems that this is exactly what <code>System.nanoTime()</code> does in Java, but I did not find a corresponding Python function, even though it should (hardware-technically) be easier to provide than <code>time.time()</code>.</p>
  
  <p>Edit: To avoid confusion and address the answers below: This is not about DST changes, and I don't want CPU time either - I want elapsed physical time. It doesn't need to be very fine-grained, and not even particularly accurate. It just shouldn't give me negative durations, or durations which are off by several orders of magnitude (above the granularity), just because someone decided to set the system clock to a different value. Here's what the Python docs say about 'time.time()':</p>
  
  <p><a href="http://docs.python.org/library/time.html#time.time" rel="noreferrer">"While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls"</a></p>
  
  <p>This is exactly what I want to avoid, since it can lead to strange things like negative values in time calculations. I can work around this at the moment, but I believe it is a good idea to learn using the proper solutions where feasible, since the kludges will come back to bite you one day.</p>
  
  <p>Edit2: Some research shows that you can get a system time independent measurement like I want in Windows by using GetTickCount64(), under Linux you can get it in the return value of times(). However, I still can't find a module which provides this functionality in Python.</p>