找出 python 脚本完成执行所需的时间
我在 python 脚本中有以下代码:
def fun():
#Code here
fun()
我想执行这个脚本,并找出执行时间(以分钟为单位)。我如何知道该脚本执行花费了多少时间?一个例子将非常感激。
I have the following code in a python script:
def fun():
#Code here
fun()
I want to execute this script and also find out how much time it took to execute in minutes. How do I find out how much time it took for this script to execute ? An example would be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您在 Linux 或 UNIX 上从命令行执行脚本吗?在这种情况下,你可以使用
Do you execute the script from the command line on Linux or UNIX? In that case, you could just use
我通常做的是使用
time
库中的clock()
或time()
。clock
测量解释器时间,而time
测量系统时间。其他注意事项可以在文档中找到。例如,
或者,您可以使用
timeit
。我经常使用time
方法,因为我可以很快地完成它,但如果您要对可隔离的代码片段进行计时,timeit
会派上用场。从timeit docs,
然后要转换为分钟,您可以简单地除以60。如果如果您希望脚本运行时采用易于阅读的格式,无论是秒还是天,您可以将其转换为
timedelta
和str
:这样就会打印出以下内容形式<代码>[D天[s],][H]H:MM:SS[.UUUUUU]。您可以查看 timedelta 文档。
最后,如果您真正想要的是分析代码,Python 提供了 配置文件库也是如此。
What I usually do is use
clock()
ortime()
from thetime
library.clock
measures interpreter time, whiletime
measures system time. Additional caveats can be found in the docs.For example,
Or alternatively, you can use
timeit
. I often use thetime
approach due to how fast I can bang it out, but if you're timing an isolate-able piece of code,timeit
comes in handy.From the timeit docs,
Then to convert to minutes, you can simply divide by 60. If you want the script runtime in an easily readable format, whether it's seconds or days, you can convert to a
timedelta
andstr
it:and that'll print out something of the form
[D day[s], ][H]H:MM:SS[.UUUUUU]
. You can check out the timedelta docs.And finally, if what you're actually after is profiling your code, Python makes available the profile library as well.
前面的代码对我来说没有问题!
The previous code works for me with no problem !
使用 timeit 模块。这很容易。运行您的 example.py 文件,使其在 Python Shell 中处于活动状态,您现在应该能够在 shell 中调用您的函数。尝试一下看看它是否有效
好,有效,现在导入 timeit 并设置一个计时器
现在我们已经设置了计时器,我们可以看到需要多长时间
然后我们开始,它会告诉您多少秒(或更短)执行该函数需要花费时间。如果它是一个简单的函数,那么您可以将其增加到 t.timeit(number=1000) (或任何数字!),然后将答案除以数字以获得平均值。
我希望这有帮助。
Use the timeit module. It's very easy. Run your example.py file so it is active in the Python Shell, you should now be able to call your function in the shell. Try it out to check it works
Good, that works, now import timeit and set up a timer
Now we have our timer set up we can see how long it takes
And there we go, it will tell you how many seconds (or less) it took to execute that function. If it's a simple function then you can increase it to t.timeit(number=1000) (or any number!) and then divide the answer by the number to get the average.
I hope this helps.
纯 Python
更好的是
time.perf_counter()
:由 这个人推荐 以及 (5:30)。
文档:
Jupyter Notebook:
%timeit
&%time
如果您使用的是 Jupyter Notebook(例如 Google Colab ),您可以使用 IPython Magic Commands。
示例:
然后在一个单独的单元格中,对函数计时多次:
输出:
仅对函数计时一次:
输出:
您可以找到有关 Magic Commands 的更多信息 此处。
Pure Python
Better yet is
time.perf_counter()
:Recommended by this guy as well (5:30).
Docs:
Jupyter Notebook:
%timeit
&%time
If you are working in a Jupyter Notebook (such as Google Colab), you can use IPython Magic Commands.
Example:
Then in a separate cell, to time the function multiple times:
Output:
To time the function only once:
Output:
You can find more about Magic Commands here.
这是我使用函数式编程来实现这一点的方法 - 没有全局名称空间污染,复数的正确变形,只使用一个装饰器,所有这些都只有三个导入。我的所有脚本中都有此功能:
示例输入:
输出:
示例输入:
输出:
Here is the way I do this using functional programming - no global namespace pollution, proper inflection of plurals, only one decorator used, all with only three imports. I have this function in all my scripts:
Sample input:
Output:
Sample input:
Output:
使用时间和日期时间包。
如果有人想执行这个脚本,并找出执行所需的时间(以分钟为单位)
上面的代码对我有用。我希望这有帮助。
use the time and datetime packages.
if anybody want to execute this script and also find out how much time it took to execute in minutes
The above code works for me. I hope this helps.
对于Windows用户。
如果您想记录执行完整Python文件的时间,请使用下面给出的简单代码。
For Windows users.
If you want to record the time for executing a complete Python file, use the simple code given below.
导入 timeit 模块并将您想要计时的脚本部分定义为函数(例如 main())。
该函数将执行 1 次(次数=1),并测量所需的时间。
Import timeit module and define the part of the script you want to time as function (e.g. main()).
The function will be executed 1 time (number=1) and the time needed will be measured.