Is there an easy way / module to correctly measure elapsed time in python? I know that I can just call time.time() twice and account for the difference, but this will lead to incorrect results if the system time is changed. Of course, this does not happen very often, but it indicates that I am measuring the wrong thing.
Using time.time() to measure duration is incredibly circular when you think about it. You accept the difference between two measurements of absolute time, which, in turn, are built according to measurements of duration (performed by timers) and known absolute times (set manually or via ntp), which does not interest you at all.
So, is there a way to request this "timer time" directly? I would suggest that it can be represented as a millisecond or microsecond, which does not have a meaningful absolute representation (and therefore, it does not need to be configured with system time). Looking back a bit, it seems that this is exactly what System.nanoTime() does in Java, but I did not find the corresponding Python function, although it should (technically and technically) be simpler than time.time() .
Edit: To avoid confusion and answer the answers below: this is not about DST changes, and I also do not want CPU time - I want the elapsed physical time. It does not have to be very fine-grained or even not particularly accurate. It just should not give me a negative duration, or a duration that is disabled several orders of magnitude (higher than granularity), just because someone decided to set the system clock to a different value. Here is what Python docs says about "time.time ()":
"Although this function usually returns non-decreasing values, it can return a lower value than the previous call if the system clock was set back between the two calls"
This is exactly what I want to avoid, as it can lead to strange things, such as negative values ββin time calculations. I can get around this for now, but I think it's a good idea to learn how to use the right solutions where possible, as kludges will return to bite you one day.
Edit2: Some research shows that you can get the system-independent dimension that I want on Windows with GetTickCount64 (), on Linux you can get it in the return value of times (). However, I still cannot find a module that provides this functionality in Python.