Skip to content

Use GetSystemTimeAsFileTime() to get a resolution of 100 ns on Windows #58053

Closed

Description

@vstinner
BPO 13845
Nosy @amauryfa, @abalkin, @vstinner, @merwok
Files
  • GetSystemTimeAsFileTime.patch
  • 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 = None
    closed_at = <Date 2012-02-07.22:41:16.078>
    created_at = <Date 2012-01-24.00:02:45.144>
    labels = []
    title = 'Use GetSystemTimeAsFileTime() to get a resolution of 100 ns on Windows'
    updated_at = <Date 2012-02-08.21:52:46.028>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2012-02-08.21:52:46.028>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-02-07.22:41:16.078>
    closer = 'vstinner'
    components = []
    creation = <Date 2012-01-24.00:02:45.144>
    creator = 'vstinner'
    dependencies = []
    files = ['24364']
    hgrepos = []
    issue_num = 13845
    keywords = ['patch']
    message_count = 12.0
    messages = ['151865', '151866', '151874', '151892', '151894', '151897', '151901', '152036', '152298', '152817', '152889', '152911']
    nosy_count = 6.0
    nosy_names = ['amaury.forgeotdarc', 'belopolsky', 'vstinner', 'eric.araujo', 'rosslagerwall', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue13845'
    versions = ['Python 3.3']

    Activity

    1. vstinner commented on Jan 24, 2012

      @vstinner
      MemberAuthor

      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.

    2. vstinner commented on Jan 24, 2012

      @vstinner
      MemberAuthor

      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().

    3. vstinner commented on Jan 24, 2012

      @vstinner
      MemberAuthor

      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).

    4. amauryfa commented on Jan 24, 2012

      @amauryfa
      Contributor

      GetSystemTimeAsFileTime() represent durations as multiple of 100ns, unfortunately its value is only updated every 15ms or so. Precision is not accuracy...

    5. vstinner commented on Jan 24, 2012

      @vstinner
      MemberAuthor

      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.

    6. amauryfa commented on Jan 24, 2012

      @amauryfa
      Contributor

      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().

    7. vstinner commented on Jan 24, 2012

      @vstinner
      MemberAuthor

      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.

    8. vstinner commented on Jan 26, 2012

      @vstinner
      MemberAuthor

      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!

    9. vstinner commented on Jan 30, 2012

      @vstinner
      MemberAuthor

      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().

    10. python-dev commented on Feb 7, 2012

      python-devmannequin
      Mannequin

      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

    11. merwok commented on Feb 8, 2012

      @merwok
      Member

      Please make sure to say “on Windows” in NEWS and commit messages when you’re doing platform-specific changes :)

    12. python-dev commented on Feb 8, 2012

      python-devmannequin
      Mannequin

      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

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      No labels
      No labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions