How to find statistics and code runtime in python

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.

+4
source share
2 answers

It looks like you are asking how the programming interface is for the timeit module. This is described here . You will need a set of samples for calculating statistics, such as min, max, average, etc., Which means that you perform multiple calculations through the Timeit class repetition method included in the timeit module.

For instance:

 timer = timeit.Timer(calculation) results = timer.timeit(10000) 
+1
source

I think you are asking how to use cProfile from code. This is actually pretty easy. cProfile.Profile has two undocumented methods, enable and disable , which you can use to start and stop the profiler. This means that you can easily create a context manager or decorator. The following recipe implements both of them in one object and includes a method for processing and printing output using the pstat module.

 import cProfile, pstats, functools class profile: def __init__(self, filename=None): """ If defined, output is written to *filename*, otherwise it is processed using *pstats* and printed to STDOUT. """ self._filename = filename self._prof = cProfile.Profile() def __enter__(self): self._prof.enable() def __exit__(self, exc_type, exc_value, traceback): self._prof.disable() if self._filename is not None: self._prof.dump_stats(self._filename) else: stats = pstats.Stats(self._prof) self.print_stats(stats) def print_stats(self, stats): """ This method processes the stats and prints them. """ stats.strip_dirs().sort_stats('cumulative').print_stats(20) def __call__(self, func): self._func = func @functools.wraps(func) def wrapper(*args, **kwargs): with self: return func(*args, **kwargs) return wrapper 

So using:

 @profile() def calculation(): a = 2 b = 3 res = a + b return res calculation() 

or

 with profile('output_file.pstat'): calculation() 

You can change print_stats , if necessary, to show the desired result.

0
source

Source: https://habr.com/ru/post/1445929/


All Articles