You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use GetSystemTimeAsFileTime() to get a resolution of 100 ns on Windows #58053
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2012-02-07.22:41:16.078>created_at=<Date2012-01-24.00:02:45.144>labels= []
title='Use GetSystemTimeAsFileTime() to get a resolution of 100 ns on Windows'updated_at=<Date2012-02-08.21:52:46.028>user='https://github.com/vstinner'
Python implements time.time() using gettimeofday() which has only a resolution of 1 microsecond because it uses the timeval structure which is only able to store microseconds.
Attached patch changes _PyTime_gettimeofday() to make it uses the timespec structure (which has a resolution has 1 nanosecond) and use GetSystemTimeAsFileTime() on Windows. So time.time() has a theorical resolution 10 times better than currently.
Oops, my first patch contains an unrelated change for Windows.
New patch fixes this bug, and change time_clock() to reuse time_time() if time_clock() fails to get the CPU frequency (unlikely) because it has a better resolution than clock().
See also bpo-11457 for discussion on nanosecond resolution and a potential new type to avoid loose of resolution of the Python float type (IEEE binary64).
GetSystemTimeAsFileTime() represent durations as multiple of 100ns, unfortunately its value is only updated every 15ms or so. Precision is not accuracy...
GetSystemTimeAsFileTime() represent durations as multiple of 100ns, unfortunately its value is only updated every 15ms or so. Precision is not accuracy...
NtSetTimerResolution is a system-wide change, and may have impact on other running applications. It may be an option to set it during the execution of profile.run() for example, but I would not enable it just to call time.clock().
NtSetTimerResolution is a system-wide change, and may have impact on other running applications. It may be an option to set it during the execution of profile.run() for example, but I would not enable it just to call time.clock().
I was not proposing to call, but it was trying to say that under
certain circumstances, you may have a better resolution than 15 ms.
Python should not limit the resolution if the OS provides a better
resolution.
Using the patch of bpo-13882, I realize that time.time() has a resolution of 1 millisecond (10^-3) and not of a microsecond (10^-6) on Windows! Windows doesn't provide gettimeofday(). Using GetSystemTimeAsFileTime() would provide a much better resolution!
Using the NTP daemon, the resolution is 1 ms instead of 15 ms (on Windows Seven). It looks like it is possible to have a resolution of 0.5 ms, but I failed to get this resolution.
Attached patch is much more simple than the previous one: it only changes _PyTime_gettimeofday().
Activity
vstinner commentedon Jan 24, 2012
Python implements time.time() using gettimeofday() which has only a resolution of 1 microsecond because it uses the timeval structure which is only able to store microseconds.
Attached patch changes _PyTime_gettimeofday() to make it uses the timespec structure (which has a resolution has 1 nanosecond) and use GetSystemTimeAsFileTime() on Windows. So time.time() has a theorical resolution 10 times better than currently.
vstinner commentedon Jan 24, 2012
Oops, my first patch contains an unrelated change for Windows.
New patch fixes this bug, and change time_clock() to reuse time_time() if time_clock() fails to get the CPU frequency (unlikely) because it has a better resolution than clock().
vstinner commentedon Jan 24, 2012
See also bpo-11457 for discussion on nanosecond resolution and a potential new type to avoid loose of resolution of the Python float type (IEEE binary64).
amauryfa commentedon Jan 24, 2012
GetSystemTimeAsFileTime() represent durations as multiple of 100ns, unfortunately its value is only updated every 15ms or so. Precision is not accuracy...
vstinner commentedon Jan 24, 2012
It is possible to improve the accuracy of this clock using the
undocumented NtSetTimerResolution() function:
http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Time/NtSetTimerResolution.html
There are applications using this undocumented function. For example:
http://www.lucashale.com/timer-resolution/
See also the timeBeginPeriod() function:
http://msdn.microsoft.com/en-us/library/ms713413%28VS.85%29.aspx
The user may have a special hardware (and its driver) or a special
softwared (ntpd?) with a better accuracy.
amauryfa commentedon Jan 24, 2012
NtSetTimerResolution is a system-wide change, and may have impact on other running applications. It may be an option to set it during the execution of profile.run() for example, but I would not enable it just to call time.clock().
vstinner commentedon Jan 24, 2012
I was not proposing to call, but it was trying to say that under
certain circumstances, you may have a better resolution than 15 ms.
Python should not limit the resolution if the OS provides a better
resolution.
vstinner commentedon Jan 26, 2012
Using the patch of bpo-13882, I realize that time.time() has a resolution of 1 millisecond (10^-3) and not of a microsecond (10^-6) on Windows! Windows doesn't provide gettimeofday(). Using GetSystemTimeAsFileTime() would provide a much better resolution!
vstinner commentedon Jan 30, 2012
Two articles (Microsoft and IBM) about high resolution time on Windows:
http://msdn.microsoft.com/en-us/magazine/cc163996.aspx
http://www.ibm.com/developerworks/library/i-seconds/
I installed the Windows port of the NTP daemon:
http://www.meinberg.de/english/sw/ntp.htm
Using the NTP daemon, the resolution is 1 ms instead of 15 ms (on Windows Seven). It looks like it is possible to have a resolution of 0.5 ms, but I failed to get this resolution.
Attached patch is much more simple than the previous one: it only changes _PyTime_gettimeofday().
python-dev commentedon Feb 7, 2012
New changeset bee7943d38c6 by Victor Stinner in branch 'default':
Issue bpo-13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
http://hg.python.org/cpython/rev/bee7943d38c6
merwok commentedon Feb 8, 2012
Please make sure to say “on Windows” in NEWS and commit messages when you’re doing platform-specific changes :)
python-dev commentedon Feb 8, 2012
New changeset 3965ed809a85 by Victor Stinner in branch 'default':
Issue bpo-13845: Fix NEWS entry, the change is specific to Windows
http://hg.python.org/cpython/rev/3965ed809a85