简体   繁体   中英

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

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 .

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM