Re: How to measure execution time of a program

Discussion in 'Python' started by Fredrik Lundh, Jun 28, 2006.

  1. Girish Sahani wrote:

    > Can anyone tell me the simplest way to do it (some code snippet that
    > could be included in the program's main function) ??


    simplest way:

    t0 = time.time()
    main()
    print time.time() - t0, "seconds"

    (assuming that you want to measure wall time, and that your program runs
    for at least a tenth of second, or so. for benchmarking of short code snippets,
    see the "timeit" module)

    </F>
     
    Fredrik Lundh, Jun 28, 2006
    #1
    1. Advertising

  2. Fredrik Lundh

    Pete Forman Guest

    "Fredrik Lundh" <> writes:
    > simplest way:
    >
    > t0 = time.time()


    You can get better resolution by using time.clock() instead of
    time.time().
    --
    Pete Forman -./\.- Disclaimer: This post is originated
    WesternGeco -./\.- by myself and does not represent
    -./\.- opinion of Schlumberger, Baker
    http://petef.port5.com -./\.- Hughes or their divisions.

    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
     
    Pete Forman, Jun 28, 2006
    #2
    1. Advertising

  3. Pete Forman wrote:

    > > t0 = time.time()

    >
    > You can get better resolution by using time.clock() instead of
    > time.time().


    depends on the platform, and whether you want wall time or process time.

    </F>
     
    Fredrik Lundh, Jun 28, 2006
    #3
  4. On 2006-06-28, Pete Forman <> wrote:
    > "Fredrik Lundh" <> writes:
    > > simplest way:
    > >
    > > t0 = time.time()

    >
    > You can get better resolution by using time.clock() instead of
    > time.time().


    Oh really? When I do it, time.clock() is worse:

    ------------------------------timeit.py------------------------------
    import time

    for i in range(5):
    t0 = time.time()
    print "hi there"
    print time.time()-t0

    for i in range(5):
    t0 = time.clock()
    print "hi there"
    print time.clock()-t0
    ------------------------------timeit.py------------------------------

    hi there
    0.000149011611938
    hi there
    4.10079956055e-05
    hi there
    3.981590271e-05
    hi there
    3.981590271e-05
    hi there
    3.88622283936e-05
    hi there
    0.0
    hi there
    0.0
    hi there
    0.0
    hi there
    0.0
    hi there
    0.0


    --
    Grant Edwards grante Yow! I'm a nuclear
    at submarine under the
    visi.com polar ice cap and I need
    a Kleenex!
     
    Grant Edwards, Jun 28, 2006
    #4
  5. Grant Edwards wrote:

    >> You can get better resolution by using time.clock() instead of
    >> time.time().

    >
    > Oh really? When I do it, time.clock() is worse:


    on Unix, time.clock() is a tick counter; if your process is running when the tick
    interrupt arrives, the internal counter value is incremented (whether the process
    actually used the full tick slot or not). the tick resolution is usually 1-10 milli-
    seconds.

    on Windows, time.clock() is a high-resolution CPU counter, with microsecond
    resolution.

    </F>
     
    Fredrik Lundh, Jun 28, 2006
    #5
    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. Peng Yu
    Replies:
    7
    Views:
    378
    Anoop Aryal
    Mar 29, 2005
  2. Girish Sahani

    How to measure execution time of a program

    Girish Sahani, Jun 28, 2006, in forum: Python
    Replies:
    0
    Views:
    360
    Girish Sahani
    Jun 28, 2006
  3. Girish Sahani
    Replies:
    1
    Views:
    315
    Wolfram Kraus
    Jun 28, 2006
  4. Shadow of Socrates
    Replies:
    0
    Views:
    435
    Shadow of Socrates
    Jan 22, 2008
  5. Shadow of Socrates

    [C#] How to measure the time of your code's execution

    Shadow of Socrates, Jan 22, 2008, in forum: C Programming
    Replies:
    0
    Views:
    288
    Shadow of Socrates
    Jan 22, 2008
Loading...

Share This Page