0

I'm reading Learning Python by Mark Lutz and in the chapter related to timing he uses the function time.time().

Reading the documentation about this function it says:

Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second.

Now because some tests return number results as 0.004040.., 0.00037289...and so on, I asked myself, how can I get a such good precision if the documentation says that not all systems provide a better precision than 1 second? How can I understand what precision my system has?

One thing I don't understand. If in every computer there is a circuit dedicated to the real-time clock (here it is said so Understanding time.perf_counter() and time.process_time()) how can the timer precision differ from platform (i.e from unix or win) should that be always the same? I mean if the circuit refreshes its counter every 1us shouldn't the precision be 1us regardless of the platform?

Community
  • 1
  • 1
zer0uno
  • 7,521
  • 13
  • 57
  • 86
  • consider using [`timeit.default_timer()`](https://docs.python.org/2/library/timeit.html#timeit.default_timer) (defined [here](http://hg.python.org/cpython/file/2.7/Lib/timeit.py#l70)) which will use the best timer available. – mgilson Sep 12 '14 at 17:34
  • One thing I don't understand. If in every computer there is a circuit dedicated to the real-time clock (here it is said so http://stackoverflow.com/questions/25785243/understanding-time-perf-counter-and-time-process-time) how can the timer precision differ from platform (i.e from unix or win) should that be always the same? I mean if the circuit refresh its counter every 1us shouldn't the precision be 1us regardless of the platform? – zer0uno Sep 12 '14 at 18:04
  • `I mean if the circuit refresh its counter every 1us shouldn't the precision be 1us regardless of the platform?` You'd think so, but different OSes have different philosophies as to how they deal with what the underlying hardware provides. As already mentioned, the Python time functions depend on the underlying C time functions, and they have history going back 4 decades. See what P.J. Plauger of the ANSI C committee has to say on this topic [here](http://booksbw.com/index.php?id1=4&category=computers&author=pluger-pj&book=1992&page=179). – PM 2Ring Sep 12 '14 at 19:14

0 Answers0