time.clock()

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

    #1

    time.clock()

    The manual says:

    On Unix, return the current processor time as a
    floating point number expressed in seconds.

    So I ran this program:

    #!/usr/bin/python

    import time

    while 1:
    print time.clock()



    This gave me a stream of floats, the integer part of which
    only updated about every three seconds. Now, the manual
    also states:

    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

    So I "man 3 clock" and notice:

    The value returned is the CPU time used so far as a clock_t; to get the number
    of seconds used, divide by CLOCKS_PER_SEC.

    So, I'm wondering how to get that value from python. All I
    really want to do is know current time relative to a given
    point so that I can capture MIDI events, and store the time
    at which they arrive. Am I barking up the wrong tree?

    Thanks,

    Toby

    --
    Posted via a free Usenet account from http://www.teranews.com

  • El Duderino

    #2
    Re: time.clock()

    Tobiah wrote:
    Am I barking up the wrong tree?
    I don't think so, time.clock() has always worked fine for me. You can
    also try time.time(). It is not as precise, but it might be sufficient
    for your needs.

    Comment

    • Benjamin Niemann

      #3
      Re: time.clock()

      Tobiah wrote:
      The manual says:
      >
      On Unix, return the current processor time as a
      floating point number expressed in seconds.
      >
      So I ran this program:
      >
      #!/usr/bin/python
      >
      import time
      >
      while 1:
      print time.clock()
      >
      >
      >
      This gave me a stream of floats, the integer part of which
      only updated about every three seconds. Now, the manual
      also states:
      >
      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
      >
      So I "man 3 clock" and notice:
      >
      The value returned is the CPU time used so far as a clock_t; to get the
      number
      of seconds used, divide by CLOCKS_PER_SEC.
      >
      So, I'm wondering how to get that value from python. All I
      really want to do is know current time relative to a given
      point so that I can capture MIDI events, and store the time
      at which they arrive. Am I barking up the wrong tree?
      What you want sound like the 'wall clock' time. The CPU time is the time
      that the CPU spent on executing your process. And unless the process uses
      100% of the CPU, CPU time will appear to be 'slower' than the wall clock.
      In your little program above the CPU spent about one third of the time on
      this process and the rest is used for other processes (e.g. updating the
      display).

      What you need is time.time(), if its precision is sufficient.

      --
      Benjamin Niemann
      Email: pink at odahoda dot de
      WWW: http://pink.odahoda.de/

      Comment

      • Fredrik Lundh

        #4
        Re: time.clock()

        Tobiah wrote:
        import time
        >
        while 1:
        print time.clock()
        >
        This gave me a stream of floats, the integer part of which
        only updated about every three seconds. Now, the manual
        also states:
        >
        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
        >
        So I "man 3 clock" and notice:
        >
        The value returned is the CPU time used so far as a clock_t;
        to get the number of seconds used, divide by CLOCKS_PER_SEC.
        >
        So, I'm wondering how to get that value from python.
        by calling clock(), of course.
        All I really want to do is know current time relative to a given
        point so that I can capture MIDI events, and store the time at
        which they arrive.
        if you want real time, use time.time().

        CPU time (processor time) is something different; that's based on how
        many CPU cycles your program has used up since it started (usually
        approximated by checking what process is running at regular intervals).
        if your program doesn't do anything, or, as in your example, spends
        most of its time waiting for someone else to do something, it won't
        consume many cycles.

        </F>

        Comment

        • Grant Edwards

          #5
          Re: time.clock()

          On 2006-07-14, Tobiah <toby@rcsreg.co mwrote:
          So I "man 3 clock" and notice:
          >
          The value returned is the CPU time used so far as a clock_t; to get the number
          of seconds used, divide by CLOCKS_PER_SEC.
          >
          So, I'm wondering how to get that value from python.
          What value?
          All I really want to do is know current time relative to a
          given point
          Which is not at all the same thing as CPU usage.
          so that I can capture MIDI events, and store the time at which
          they arrive.
          time.time()
          Am I barking up the wrong tree?
          Yes, but your in the right grove.

          --
          Grant Edwards grante Yow! "THE LITTLE PINK
          at FLESH SISTERS," I saw them
          visi.com at th' FLUROESCENT BULB
          MAKERS CONVENTION...

          Comment

          • Nick Craig-Wood

            #6
            Re: time.clock()

            Benjamin Niemann <pink@odahoda.d ewrote:
            Tobiah wrote:
            On Unix...
            What you want sound like the 'wall clock' time. The CPU time is the time
            that the CPU spent on executing your process. And unless the process uses
            100% of the CPU, CPU time will appear to be 'slower' than the wall clock.
            In your little program above the CPU spent about one third of the time on
            this process and the rest is used for other processes (e.g. updating the
            display).
            >
            What you need is time.time(), if its precision is sufficient.
            In linux at least time.time() has microsecond precision.
            >>for i in range(10): print "%20.6f" % time.time()
            ....
            1153130111.5664 63
            1153130111.5665 13
            1153130111.5665 35
            1153130111.5665 57
            1153130111.5665 78
            1153130111.5666 01
            1153130111.5666 21
            1153130111.5666 44
            1153130111.5666 65
            1153130111.5666 86

            Wheras time.clock() only has 10 ms precision
            >>for i in range(10): print "%20.6f" % time.clock()
            ....
            1.770000
            1.770000
            1.770000
            1.770000
            1.770000
            1.770000
            1.770000
            1.770000
            1.770000
            1.770000

            time.clock() is elapsed cpu time of just that process.

            I think the precisions are the other way round on windows.

            --
            Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

            Comment

            Working...