Finding cpu time spent on my program

Discussion in 'Python' started by jm.suresh@no.spam.gmail.com, Feb 5, 2007.

  1. Guest

    I am trying to measure the time the processor spends on some
    operation, and I want this measure to not depend on the current load
    of the machine. But doing the following prints different values each
    time a run.

    import time
    c1 = time.clock()
    for x in xrange(0xFFFFF): pass

    c2 = time.clock()
    print "Time spent is %f" % (c2-c1)

    Is there a way to measure the number of cpu cycles spent on my program
    alone irrespective of the current load etc of the machine.

    -
    Suresh
     
    , Feb 5, 2007
    #1
    1. Advertising

  2. Guest

    On Feb 5, 2:37 am, "" <>
    wrote:
    > I am trying to measure the time the processor spends on some
    > operation, and I want this measure to not depend on the current load
    > of the machine. But doing the following prints different values each
    > time a run.
    >
    > import time
    > c1 = time.clock()
    > for x in xrange(0xFFFFF): pass
    >
    > c2 = time.clock()
    > print "Time spent is %f" % (c2-c1)
    >
    > Is there a way to measure the number of cpu cycles spent on my program
    > alone irrespective of the current load etc of the machine.
    >
    > -
    > Suresh


    One of the best ways to time small snippets of code is python's timeit
    module. See the docs at http://docs.python.org/lib/module-timeit.html

    - Mike
     
    , Feb 5, 2007
    #2
    1. Advertising

  3. wrote:
    > On Feb 5, 2:37 am, "" <>
    > wrote:
    >
    >> I am trying to measure the time the processor spends on some
    >> operation, and I want this measure to not depend on the current load
    >> of the machine. But doing the following prints different values each
    >> time a run.
    >>
    >> import time
    >> c1 = time.clock()
    >> for x in xrange(0xFFFFF): pass
    >>
    >> c2 = time.clock()
    >> print "Time spent is %f" % (c2-c1)
    >>
    >> Is there a way to measure the number of cpu cycles spent on my program
    >> alone irrespective of the current load etc of the machine.
    >>

    There's an performance testing suite that you might also find useful.
    Check out TAU: Tuning and Analysis Utilities toolkit (simple google
    search will take you to the page). You can get some serious numbers
    using that thing and they have python support as well as some tools for
    automatically profiling an entire application. Check it out.

    -carl

    --

    Carl J. Van Arsdall

    Build and Release
    MontaVista Software
     
    Carl J. Van Arsdall, Feb 5, 2007
    #3
  4. <> wrote:
    > On Feb 5, 2:37 am, "" <>
    > wrote:
    > > I am trying to measure the time the processor spends on some
    > > operation, and I want this measure to not depend on the current load
    > > of the machine.

    >
    > One of the best ways to time small snippets of code is python's timeit
    > module. See the docs at
    > http://docs.python.org/lib/module-timeit.html


    Timeit is very cool, but it doesn't measure CPU time, it measure real
    time, eg on a linux box

    $ python -m timeit 'for i in xrange(100000): pass'
    100 loops, best of 3: 11.4 msec per loop

    Now run 10 copies of the same program at once

    $ for i in `seq 10` ; do python -m timeit 'for i in xrange(100000): pass' & done
    10 loops, best of 3: 24.4 msec per loop
    10 loops, best of 3: 83.2 msec per loop
    10 loops, best of 3: 83.4 msec per loop
    10 loops, best of 3: 81.4 msec per loop
    10 loops, best of 3: 83 msec per loop
    10 loops, best of 3: 60.7 msec per loop
    10 loops, best of 3: 47 msec per loop
    10 loops, best of 3: 48.6 msec per loop
    10 loops, best of 3: 42.3 msec per loop
    10 loops, best of 3: 38.7 msec per loop

    > > Is there a way to measure the number of cpu cycles spent on my program
    > > alone irrespective of the current load etc of the machine.


    The most accurate way of measuring CPU usage under linux is getrusage,
    eg

    >>> import resource
    >>> def cpu_time():

    .... return resource.getrusage(resource.RUSAGE_SELF)[0]
    ....

    Now try this out

    >>> def f():

    .... for i in xrange(100000):
    .... pass
    ....
    >>> start = cpu_time(); f(); dt = cpu_time() - start; print dt

    0.008001
    >>> start = cpu_time(); f(); dt = cpu_time() - start; print dt

    0.012001
    >>> start = cpu_time(); f(); dt = cpu_time() - start; print dt

    0.012
    >>> start = cpu_time(); f(); dt = cpu_time() - start; print dt

    0.012001
    >>> start = cpu_time(); f(); dt = cpu_time() - start; print dt

    0.016001
    >>> start = cpu_time(); f(); dt = cpu_time() - start; print dt

    0.012001
    >>> start = cpu_time(); f(); dt = cpu_time() - start; print dt

    0.008001

    You'll see the result is quantised to 4 ms (not sure what the .000001
    bits are about!)

    4ms is the clock rate of this machine, ie 250 Hz. This is a compile
    time option for the linux kernel and is usually set in the range 100
    Hz to 1000 Hz. The kernel doesn't measure CPU usage more accurately
    than this.

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Feb 6, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    0
    Views:
    380
  2. Replies:
    5
    Views:
    605
    Laurent Bugnion
    Apr 26, 2006
  3. John
    Replies:
    1
    Views:
    347
    Leor Zolman
    Apr 12, 2004
  4. xianwei
    Replies:
    20
    Views:
    721
    Guy Macon
    Sep 29, 2007
  5. lyallex
    Replies:
    8
    Views:
    383
    lyallex
    Oct 27, 2007
Loading...

Share This Page