【问题标题】:Is there a function to see how long a script takes to run?是否有查看脚本运行时间的功能?
【发布时间】:2023-02-24 16:52:16
【问题描述】:

我有一个可以对文件进行排序的 python 脚本,我想知道运行它需要多长时间。

我搜索了一个名为 time 的模块,但它没有用,它要求我输入一个实时时间。

【问题讨论】:

  • 试试标准库中的timeit模块

标签: python


【解决方案1】:

您可以这样计算执行时间:

import time

start_time = time.time()

# your script here

end_time = time.time()
execution_time = end_time - start_time

print("Execution time:", execution_time, "seconds")

【讨论】:

  • 对于粗略计时,time 会起作用,但对于准确的性能测量,则首选 timeit 模块(请参阅this answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2021-05-29
  • 2023-01-26
  • 2019-11-15
  • 2013-04-02
  • 2015-12-01
  • 2012-08-19
相关资源
最近更新 更多