1

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"perf" show cpu, memory, etc? Could you show an example?
@DiegoCardoso - it's a powerful profiler for linux, it wouldn't show python stack traces, only the native ones. The best tutorial I've come across is brendangregg.com/perf.html
0

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!

4 Comments

Thanks a lot for the help. It helped a lot. But one last time, where do I see cpu consumption and memory in the example?
I am not sure if memory consumption per statement is possible in cProfiler. Maybe you can check this: pypi.org/project/memory_profiler
Ok, i'll take a look. What about cpu consumption? thank you so much again
CPU consumption as in? You want number of CPU cycles? cProfile displays you amount of time spent per call per statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.