System Clock

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Harriman

    System Clock

    Hi,

    I need to be able to measure a time interval in milliseconds on a windows
    machine. I have tried using time.clock() but it appears to measure time in
    seconds...Is there a way to measure time more precisely?

    Regards,

    James Harriman
    v38zy@unb.ca


  • Terry Reedy

    #2
    Re: System Clock


    "James Harriman" <v38zy@unb.ca > wrote in message
    news:9SBqb.8942 $R13.490724@urs a-nb00s0.nbnet.nb .ca...[color=blue]
    > Hi,
    >
    > I need to be able to measure a time interval in milliseconds on a[/color]
    windows[color=blue]
    > machine. I have tried using time.clock() but it appears to measure[/color]
    time in[color=blue]
    > seconds...Is there a way to measure time more precisely?[/color]

    For floats, what matters is the resolution (precision), not the unit.
    On AMDK2-385, Win 98:
    [color=blue][color=green][color=darkred]
    >>> a=time.clock(); time.sleep(1); print time.clock()-a[/color][/color][/color]
    1.01412863105 # correct, seconds
    [color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -3.0171474548978 949e-005[color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -3.1847667571582 861e-005[color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -3.1009571046070 05e-005[color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -2.9333377995044 43e-005[color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -3.0171474577400 659e-005[color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -3.1009571046070 05e-005[color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -3.1009571102913 469e-005[color=blue][color=green][color=darkred]
    >>> time.clock()-time.clock()[/color][/color][/color]
    -2.9333378051887 848e-005

    This is reproducibly .03+ milleseconds (minus because earlier - later)
    == 30 microseconds.

    Now speed up a bit:[color=blue][color=green][color=darkred]
    >>> tc = time.clock
    >>> tc()-tc()[/color][/color][/color]
    -2.8495281526375 038e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -2.7657185000862 228e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -3.0171474520557 24e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -2.6819088532192 836e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -2.8495281526375 038e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -2.7657185000862 228e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -2.8495281526375 038e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -2.7657185000862 228e-005[color=blue][color=green][color=darkred]
    >>> tc()-tc()[/color][/color][/color]
    -2.7657185000862 228e-005

    The average is about 28 microsecs, so time.clock lookup appears to be
    measurebly about 1 microsecond, but anything much smaller would be
    lost in the noise on this system.

    (Oddly, I also get this:[color=blue][color=green][color=darkred]
    >>> tc()-tc(); tc()-tc()[/color][/color][/color]
    -2.8495281412688 2e-005
    -1.4247640820030 938e-005
    which I have not figured out yet)

    Terry J. Reedy



    Comment

    • Jp Calderone

      #3
      Re: System Clock

      On Fri, Nov 07, 2003 at 12:40:37AM +0000, James Harriman wrote:[color=blue]
      > Hi,
      >
      > I need to be able to measure a time interval in milliseconds on a windows
      > machine. I have tried using time.clock() but it appears to measure time in
      > seconds...Is there a way to measure time more precisely?[/color]

      From http://www.python.org/doc/lib/module-time.html

      clock()
      On Unix, return the current processor time as a floating point number
      expressed in seconds. The precision, and in fact the very definition of the
      meaning of `processor time'' , depends on that of the C function of the same
      name, but in any case, this is the function to use for benchmarking Python
      or timing algorithms.

      On Windows, this function returns wall-clock seconds elapsed since the
      first call to this function, as a floating point number, based on the Win32
      function QueryPerformanc eCounter(). The resolution is typically better than
      one microsecond.

      time()
      Return the time as a floating point number expressed in seconds since
      the epoch, in UTC. Note that even though the time is always returned as a
      floating point number, not all systems provide time with a better precision
      than 1 second. While this function normally returns non-decreasing values,
      it can return a lower value than a previous call if the system clock has
      been set back between the two calls.

      Jp

      Comment

      • Jp Calderone

        #4
        Re: System Clock

        On Thu, Nov 06, 2003 at 10:47:19PM -0500, Terry Reedy wrote:[color=blue]
        >
        > [snip]
        >
        > (Oddly, I also get this:[color=green][color=darkred]
        > >>> tc()-tc(); tc()-tc()[/color][/color]
        > -2.8495281412688 2e-005
        > -1.4247640820030 938e-005
        > which I have not figured out yet)
        >[/color]

        I think this indicates there is more than one cost path down into the timing
        machinery. Consider:
        [color=blue][color=green][color=darkred]
        >>> time()-time();time()-time();time()-time();time()-time();time()-time();time()-time();time()-time();time()-time();time()-time();time()-time()[/color][/color][/color]
        -3.9339065551757 812e-06
        -9.5367431640625 e-07
        0.0
        -1.0728836059570 312e-06
        -1.0728836059570 312e-06
        -1.0728836059570 312e-06
        -9.5367431640625 e-07
        -9.5367431640625 e-07
        0.0
        0.0

        It appears to me as though making subsequent calls to time.time() in rapid
        succession allows one to exploit an optimization somewhere.

        Perhaps some hardware-level caching?

        Jp

        Comment

        Working...