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

8
  • do u face any problem with it ? Commented Jul 22, 2016 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. Commented Jul 22, 2016 at 19:17
  • Yes using the virtual time kernel github.com/littlepretty/VirtualTimeKernel Which relies on GToD Commented Jul 22, 2016 at 19:19
  • Also the docs are misleading suggesting that if available it should use GToD docs.python.org/3.4/library/time.html Commented Jul 22, 2016 at 19:23
  • The documentation is not lying; perhaps you need to build Python directly on your system instead? See the pygettimeofday() function specifically. Commented Jul 22, 2016 at 19:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the info. I will try to force it to use GToD as its clocking mechanism.
@channon: not sure why you'd need to as I see VirtualTimeKernel supports clock_gettime().
because the virtual time only effects GToD it is in the code for that function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.