Diego
Diego

Reputation: 21

benchmarking python (command line)

I have a program in python 3.6 that I made to run in linux. I needed to know how much cpu, memory, etc., it consumed when it was executed (in command line), could you help me?

Thank you

Note: Sorry for the tags used, I was not sure which ones to put

Upvotes: 1

Views: 820

Answers (2)

return007
return007

Reputation: 843

For timing single lines, you can python's magic function %timeit. (You can also time multiple lines, however it would give result for complete execution and not per statement basis)

However for a detailed description, you can use cProfile. You can read the description here.

Sample code that might help you:

[sample.py]

import time
print('Hello!')
time.sleep(2)
print('Thanks for waiting!')

cProfile can help you profile your program written in sample.py. Run your python file like below from your linux terminal.

user@this-pc$ python3 -m cProfile sample.py 

Output:

Hello!
Thanks for waiting!
         6 function calls in 2.001 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.001    2.001 sample.py:1(<module>)
        1    0.000    0.000    2.001    2.001 {built-in method builtins.exec}
        2    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    2.001    2.001    2.001    2.001 {built-in method time.sleep}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Hope this helps you.

Cheers!

Upvotes: 0

bobah
bobah

Reputation: 18864

For basic experiments you can use %timeit with the ipython interpreter. For high precision low level ones - perf. For everything in between there is an article specifically on the topic in the documentation.

Upvotes: 1

Related Questions