【发布时间】:2020-09-22 04:29:11
【问题描述】:
我试图弄清楚如何将一些括号括在我已经编写的代码段周围,以替代多行 timeit 示例中的三重括号。
这很好用:
import timeit
stmt = """\
a = bunch * of
fancy = math ** operations
tada = function(a, fancy)
"""
print("seconds = ", timeit.timeit(stmt=stmt, number=1, setup="import numpy as np; import pandas as pd"))
来源:https://docs.python.org/3.10/library/timeit.html#examples
我不知道如何让这些三引号在我创建的自定义函数中发挥出色。
但我想把它精简成这样:
fancy_timing_function(
a = bunch * of
fancy = math ** operations
tada = function(a, fancy)
)
输出是不错的3 minutes 21.40 seconds
感谢社区提供的任何帮助、建议或替代解决方案(也许有人已经想到了类似的东西)。提前致谢。
【问题讨论】:
-
@hiroprotagonist 我很欣赏这个建议,但是当我尝试它时,它不适用于多行代码。例如time.perf_count(花式数学)
-
是的,您只能在函数上使用它。你必须写一个收集你想要计时的操作。 (例如:您不能将赋值用作函数参数)。
标签: python benchmarking timing