3

I am wondering why python 2.7 uses gettimeofday() when running time.time() but yet in python 3.4 it does not?

It appears when running strace that it may be querying /etc/localtime

Will
  • 24,082
  • 14
  • 97
  • 108
channon
  • 362
  • 3
  • 16
  • do u face any problem with it ? – Indra Uprade Jul 22 '16 at 19:12
  • Is there any problem with the different implementation? The Python 3 `time` module was overhauled when the various new clock types were added. – Martijn Pieters Jul 22 '16 at 19:17
  • Yes using the virtual time kernel https://github.com/littlepretty/VirtualTimeKernel Which relies on GToD – channon Jul 22 '16 at 19:19
  • Also the docs are misleading suggesting that if available it should use GToD https://docs.python.org/3.4/library/time.html – channon Jul 22 '16 at 19:23
  • The documentation is not lying; perhaps you need to build Python directly on your system instead? See the [`pygettimeofday()` function](https://hg.python.org/cpython/file/3.5/Python/pytime.c#l451) specifically. – Martijn Pieters Jul 22 '16 at 19:45
  • Specifically, what does `import time; print(time.get_clock_info('time'))` produce for your system? – Martijn Pieters Jul 22 '16 at 19:47
  • namespace(adjustable=True, implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, resolution=1e-09) – channon Jul 22 '16 at 19:57
  • @channon: right, so your system uses the newer `clock_gettime()` function, as it well should. I see that `VirtualTimeKernel` supports that function too. – Martijn Pieters Jul 22 '16 at 20:00

1 Answers1

2

Python 3 will use gettimeofday() when your system has been detected to support this at compile time. However, on POSIX systems it'll only use that if clock_gettime(CLOCK_REALTIME) is not available instead; according to the POSIX 2008 standard the latter is preferred as gettimeofday() is considered obsolete.

At runtime, you can query what Python thought your system could support at compile time by using the time.get_clock_info() function, which returns a namedtuple instance with a implementation field:

implementation: The name of the underlying C function used to get the clock value

On my OSX 10.11 system, for the 'time' clock, that produces gettimeofday():

>>> time.get_clock_info('time').implementation
'gettimeofday()'

You can read through the pygettimeofday() C implementation to see what implementations may be used; on Windows GetSystemTimeAsFileTime() is used for example.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for the info. I will try to force it to use GToD as its clocking mechanism. – channon Jul 22 '16 at 20:15
  • @channon: not sure why you'd need to as I see [VirtualTimeKernel supports `clock_gettime()`](https://github.com/littlepretty/VirtualTimeKernel/blob/7d3b11be676df5e828f9c52b34990ebc55330e72/arch/x86/vdso/vclock_gettime.c). – Martijn Pieters Jul 22 '16 at 20:52
  • because the virtual time only effects GToD it is in the code for that function – channon Jul 22 '16 at 20:55