I am working on python and came across some concept of finding statistics and code runtime
Suppose I had the following code
from time import gmtime, strftime import timeit def calculation(): a = 2 b = 3 res = a + b return res if 'name' == 'main' : exec_time = timeit.timeit(calculation) print exec_time
result:
0.2561519145965576
So, from the above code, I can find the code execution time, but how to find the code statistics in python?
Finally, my intention is below the points
- How to find code statistics in python
- How to find runtime of all code in python
- What did code statistics really mean?
Edited Code:
For example, I had the code above in the test.py file
Now I have executed the above file using the command below
python -m cProfile test.py
Result:
sh-4.2$ python -m cProfile test.py 4 function calls in 0.001 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.001 0.001 0.001 0.001 test.py:1(<module>) 1 0.000 0.000 0.000 0.000 timeit.py:105(Timer) 1 0.001 0.001 0.001 0.001 timeit.py:53(<module>) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
So, I need something like this, when I run the above code, I try to write this statistics print function inside the test.py file instead of running the file with the python -m cProfile test.py from the terminal.
At the very least, I want to find the statistics and execution time of the calculation() function when the file is running, because in reality the calculation of the function has more functionality that performs some operation.
source share