Python performance tests - basic
Brute force (datetime
)
1 | import datetime |
Abstraction (timeit
)
Run the snippet a million times and return an average execution time as a result
Example snippet
1
[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]
Test snippet
1
2
3
4
5import timeit
# default: 1 million times
timeit.timeit("[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]")
# define the number of times
timeit.timeit("[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]", number=1000)Further ideas
- Get a list of execution times
1
2
3
4import timeit
timeit.repeat("[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]")
# get the best result
min(timeit.repeat("[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]"))
- Get a list of execution times
Statistics (cProfile
)
Example snippet
1
[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]
Test snippet
1
2import cProfile
cProfile.run("[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]")Results data (Row = function that was executed)
1
2
3
4
5
6
7
8
94 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<listcomp>)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '\_lsprof.Profiler' objects}ncalls
: the number of times that particular function was called- This number may actually be written as a fraction (e.g. 3/1) where the first value is the number of total calls and the second value is the number of primitive calls (not recursive).
tottime
: total amount of time the function spent executing not including calls to subfunctions.percall
(first): the ratio of tottime to ncalls (i.e. the average amount of time spent in this function excluding subfunctions).cumtime
: the total amount of time the function spent executing including calls to subfunctions.percall
(second): the ratio of cumtime to primitive calls (i.e. the average amount of time spent in this function).filename:lineno(function)
: filename, line number, and function.