This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use GetSystemTimeAsFileTime() to get a resolution of 100 ns on Windows
Type: Stage:
Components: Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, eric.araujo, python-dev, rosslagerwall, vstinner
Priority: normal Keywords: patch

Created on 2012-01-24 00:02 by vstinner, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
GetSystemTimeAsFileTime.patch vstinner, 2012-01-30 01:38 review
Messages (12)
msg151865 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-01-24 00:02
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.
msg151866 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-01-24 00:11
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().
msg151874 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-01-24 01:27
See also #11457 for discussion on nanosecond resolution and a potential new type to avoid loose of resolution of the Python float type (IEEE binary64).
msg151892 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-01-24 10:48
GetSystemTimeAsFileTime() represent durations as multiple of 100ns, unfortunately its value is only updated every 15ms or so.  Precision is not accuracy...
msg151894 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-01-24 11:22
> GetSystemTimeAsFileTime() represent durations as multiple of 100ns, unfortunately its value is only updated every 15ms or so.  Precision is not accuracy...

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.
msg151897 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-01-24 11:49
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().
msg151901 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-01-24 13:16
> 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.
msg152036 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-01-26 21:51
Using the patch of #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!
msg152298 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-01-30 01:38
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().
msg152817 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-02-07 22:40
New changeset bee7943d38c6 by Victor Stinner in branch 'default':
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
http://hg.python.org/cpython/rev/bee7943d38c6
msg152889 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-08 16:48
Please make sure to say “on Windows” in NEWS and commit messages when you’re doing platform-specific changes :)
msg152911 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-02-08 21:52
New changeset 3965ed809a85 by Victor Stinner in branch 'default':
Issue #13845: Fix NEWS entry, the change is specific to Windows
http://hg.python.org/cpython/rev/3965ed809a85
History
Date User Action Args
2022-04-11 14:57:26adminsetgithub: 58053
2012-02-08 21:52:46python-devsetmessages: + msg152911
2012-02-08 16:48:08eric.araujosetnosy: + eric.araujo
messages: + msg152889
2012-02-07 22:41:16vstinnersetstatus: open -> closed
resolution: fixed
2012-02-07 22:40:31python-devsetnosy: + python-dev
messages: + msg152817
2012-01-31 22:37:13vstinnersetfiles: - timespec-2.patch
2012-01-30 01:38:39vstinnersetfiles: + GetSystemTimeAsFileTime.patch

messages: + msg152298
2012-01-26 21:51:21vstinnersetmessages: + msg152036
2012-01-24 13:16:59vstinnersetmessages: + msg151901
2012-01-24 11:49:47amaury.forgeotdarcsetmessages: + msg151897
2012-01-24 11:22:53vstinnersetmessages: + msg151894
2012-01-24 10:48:56amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg151892
2012-01-24 04:11:51rosslagerwallsetnosy: + rosslagerwall
2012-01-24 01:27:55vstinnersetmessages: + msg151874
2012-01-24 00:11:42vstinnersetfiles: - timespec.patch
2012-01-24 00:11:36vstinnersetfiles: + timespec-2.patch

messages: + msg151866
2012-01-24 00:02:45vstinnercreate