How do I get time of a Python program's execution?
The simplest way in Python:
import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))
This assumes that your program takes at least a tenth of second to run.
Prints:
--- 0.764891862869 seconds ---
Python time measure function
First and foremost, I highly suggest using a profiler or atleast use timeit.
However if you wanted to write your own timing method strictly to learn, here is somewhere to get started using a decorator.
Python 2:
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print '%s function took %0.3f ms' % (f.func_name, (time2-time1)*1000.0)
return ret
return wrap
And the usage is very simple, just use the @timing decorator:
@timing
def do_work():
#code
Python 3:
def timing(f):
def wrap(*args, **kwargs):
time1 = time.time()
ret = f(*args, **kwargs)
time2 = time.time()
print('{:s} function took {:.3f} ms'.format(f.__name__, (time2-time1)*1000.0))
return ret
return wrap
Note I'm calling f.func_name
to get the function name as a string(in Python 2), or f.__name__
in Python 3.
Measuring elapsed time with the Time module
start_time = time.time()
# your code
elapsed_time = time.time() - start_time
You can also write simple decorator to simplify measurement of execution time of various functions:
import time
from functools import wraps
PROF_DATA = {}
def profile(fn):
@wraps(fn)
def with_profiling(*args, **kwargs):
start_time = time.time()
ret = fn(*args, **kwargs)
elapsed_time = time.time() - start_time
if fn.__name__ not in PROF_DATA:
PROF_DATA[fn.__name__] = [0, []]
PROF_DATA[fn.__name__][0] += 1
PROF_DATA[fn.__name__][1].append(elapsed_time)
return ret
return with_profiling
def print_prof_data():
for fname, data in PROF_DATA.items():
max_time = max(data[1])
avg_time = sum(data[1]) / len(data[1])
print "Function %s called %d times. " % (fname, data[0]),
print 'Execution time max: %.3f, average: %.3f' % (max_time, avg_time)
def clear_prof_data():
global PROF_DATA
PROF_DATA = {}
Usage:
@profile
def your_function(...):
...
You can profile more then one function simultaneously. Then to print measurements just call the print_prof_data():
How to measure time taken between lines of code in python?
If you want to measure CPU time, can use time.process_time()
for Python 3.3 and above:
import time
start = time.process_time()
# your code here
print(time.process_time() - start)
First call turns the timer on, and second call tells you how many seconds have elapsed.
There is also a function time.clock()
, but it is deprecated since Python 3.3 and will be removed in Python 3.8.
There are better profiling tools like timeit
and profile
, however time.process_time() will measure the CPU time and this is what you're are asking about.
If you want to measure wall clock time instead, use time.time()
.
accurately measure time python function takes
According to the Python documentation, it has to do with the accuracy of the time function in different operating systems:
The default timer function is platform
dependent. On Windows, time.clock()
has microsecond granularity but
time.time()‘s granularity is 1/60th of
a second; on Unix, time.clock() has
1/100th of a second granularity and
time.time() is much more precise. On
either platform, the default timer
functions measure wall clock time, not
the CPU time. This means that other
processes running on the same computer
may interfere with the timing ... On Unix, you can
use time.clock() to measure CPU time.
To pull directly from timeit.py
's code:
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
In addition, it deals directly with setting up the runtime code for you. If you use time
you have to do it yourself. This, of course saves you time
Timeit's setup:
def inner(_it, _timer):
#Your setup code
%(setup)s
_t0 = _timer()
for _i in _it:
#The code you want to time
%(stmt)s
_t1 = _timer()
return _t1 - _t0
Python 3:
Since Python 3.3 you can use time.perf_counter()
(system-wide timing) or time.process_time()
(process-wide timing), just the way you used to use time.clock()
:
from time import process_time
t = process_time()
#do some stuff
elapsed_time = process_time() - t
The new function process_time
will not include time elapsed during sleep.
Python 3.7+:
Since Python 3.7 you can also use process_time_ns()
which is similar to process_time()
but returns time in nanoseconds.
Python - measure function execution time with decorator
Your function is running in 0.0001 something seconds. But since the number is so large, it takes almost 3 seconds to print it. So your code is working, I/O(print) is just slow. You can see it yourself if you only print the time it took.
To further demonstrate this and also add emil's comment
def decorator_timer(some_function):
from time import time
def wrapper(*args, **kwargs):
t1 = time()
result = some_function(*args, **kwargs)
end = time()-t1
return result, end
return wrapper
@decorator_timer
def my_pow(a, b):
res = a ** b
return res
# or just return a ** b, it doesn't really matter
result, exec_time = my_pow(99999 , 99999)
print(exec_time) # prints after 0.07347989082336426 seconds
print(result) # takes ~3 seconds
python time measure for every function
At the terminal, run
python -m profile -s time file.py
or
python -m cProfile -s time file.py
The second can be faster, and is never worse.
This will give something like:
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
39 0.132 0.003 0.139 0.004 :0(load_dynamic)
239 0.097 0.000 0.097 0.000 :0(loads)
541/1 0.083 0.000 3.556 3.556 :0(exec)
30 0.082 0.003 0.082 0.003 :0(statusBar)
... etc ...
The left hand side will contain your functions.
Using time.time() in Python to measure time elapsed for code segments consumes significant time itself
It seems this is expected behaviour.
Related Topics
How to Fix "Importerror: Dll Load Failed" While Importing Win32Api
Django JSONfield Inside Arrayfield
Redirect While Passing Arguments
Use Aws Glue Python with Numpy and Pandas Python Packages
Preprocessing in Scikit Learn - Single Sample - Depreciation Warning
Spark Iteration Time Increasing Exponentially When Using Join
Mixed Slashes with Os.Path.Join on Windows
Fix Not Load Dynamic Library for Tensorflow Gpu
Set Up Python 3 Build System with Sublime Text 3
How to Plot and Annotate Grouped Bars in Seaborn/Matplotlib
Importerror: No Module Named 'Bottle' - Pycharm
Tab Completion in Python's Raw_Input()
Iso to Datetime Object: 'Z' Is a Bad Directive
Plot a Bar Using Matplotlib Using a Dictionary
Finding Elements Not in a List
How to Replace (Or Strip) an Extension from a Filename in Python
Regex: Attributeerror: 'Nonetype' Object Has No Attribute 'Groups'