简体   繁体   English

基准python(命令行)

[英]benchmarking python (command line)

I have a program in python 3.6 that I made to run in linux.我在 python 3.6 中有一个程序,可以在 linux 中运行。 I needed to know how much cpu, memory, etc., it consumed when it was executed (in command line), could you help me?我需要知道它在执行时(在命令行中)消耗了多少 cpu、内存等,你能帮我吗?

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.对于基本实验,您可以将%timeitipython解释器一起使用。 For high precision low level ones - perf .对于高精度低级别的 - 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 .对于单行计时,您可以使用 python 的魔法函数%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.但是,对于详细说明,您可以使用 cProfile。 You can read the description here .您可以在此处阅读说明。

Sample code that might help you:可能对您有帮助的示例代码:

[sample.py] [示例.py]

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

cProfile can help you profile your program written in sample.py. cProfile 可以帮助您分析在 sample.py 中编写的程序。 Run your python file like below from your linux terminal.从你的 linux 终端运行你的 python 文件,如下所示。

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!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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