49

Basically, I want to be able to output the elapsed time that the application is running for. I think I need to use some sort of timeit function but I'm not sure which one. I'm looking for something like the following...

START MY TIMER
code for application
more code
more code
etc
STOP MY TIMER

OUTPUT ELAPSED TIME to do the above code in between start and stop of timer. Thoughts?

Blender
  • 289,723
  • 53
  • 439
  • 496
user1675111
  • 8,695
  • 4
  • 18
  • 14

8 Answers8

87

The simplest way to do it is to put:

import time
start_time = time.time()

at the start and

print "My program took", time.time() - start_time, "to run"

at the end.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
21

To get the best result on different platforms:

from timeit import default_timer as timer

# START MY TIMER
start = timer()
code for application
more code
more code
etc
# STOP MY TIMER
elapsed_time = timer() - start # in seconds

timer() is a time.perf_counter() in Python 3.3 and time.clock()/time.time() in older versions on Windows/other platforms correspondingly.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
10

If you're running Mac OS X or Linux, just use the time utility:

$ time python script.py
real    0m0.043s
user    0m0.027s
sys     0m0.013s

If not, use the time module:

import time

start_time = time.time()

# ...

print time.time() - start_time, 's'
Blender
  • 289,723
  • 53
  • 439
  • 496
9

You could use the system command time:

foo@foo:~# time python test.py
hello world!

real    0m0.015s
user    0m0.008s
sys     0m0.007s
AJ.
  • 27,586
  • 18
  • 84
  • 94
  • 2
    I like this, except this will include any "overhead" for imports and initializations you wouldn't want to test, in the case of timing a snippet. – Ian Sep 16 '12 at 15:51
7

Take system current time at start of your program, and at the end again take the end time of your program, finally subtract the two time.

Below code depicts how we can perform the above said operation:

import time
start = time.time()

sample code starts here

cnt = 1
for fun in range(10**11):
    if (fun % 10**9 == 0):
        print(cnt, fun)
        cnt += 1

sample code ends here

end = time.time()
print('Time taken for fun program: ', end - start)

Hope this is clear for ever.

Debashis Sahoo
  • 5,388
  • 5
  • 36
  • 41
0

Couldn't you just get the current system time at the start and at the end, then subtract the start time from the final time?

Patrick Mahoney
  • 555
  • 7
  • 19
0
import time as t
def TimeTakenDecorator(func):
    def wraper(*args,**kwargs):
        start = t.time()
        func(*args,**kwargs)
        end = t.time()
        print('Time taken for fun program: ', end - start)
    return wraper

@TimeTakenDecorator
def hello(s):
    for _ in range(100000000):
        pass
    print(s)
    
hello("test")
Javadirad
  • 19
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 02 '22 at 11:26
0

I would use Rad's answer with the decorator, and the main thing I would change is to make sure to use a monotonic clock, so instead of t.time() use t.monotonic(), or for greater precision t.monotonic_ns(). But this requires that you use python v3.3 or higher, otherwise you may try this ctypes variant of getting a monotonic clock.

Here's from python3 docs:

time.monotonic() → float

Return the value (in fractional seconds) of a monotonic clock, i.e. a clock that cannot go backwards. The clock is not affected by system clock updates. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.

Use monotonic_ns() to avoid the precision loss caused by the float type.

New in version 3.3.

Changed in version 3.5: The function is now always available and always system-wide.

Changed in version 3.10: On macOS, the function is now system-wide.

time.monotonic_ns() → int

Similar to monotonic(), but return time as nanoseconds.

New in version 3.7.

>>> import time as t
>>> t.time()
1666507313.6913335
>>> t.monotonic_ns()
2346741390948
>>> t.monotonic_ns()
2350236290783
>>> t.monotonic()
2353.851540444
>>> t.monotonic_ns()
2356366587038
>>> t.get_clock_info('time')
namespace(implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, adjustable=True, resolution=1e-09)
>>> t.get_clock_info('monotonic')
namespace(implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, adjustable=False, resolution=1e-09)
>>> 
>>> t.monotonic()                                                                                                                                                 
3475.071162687                                                                                                                                                    
>>> t.monotonic_ns()/10**9                                                                                                                                        
3475.877335106

Read more info here and here.