python - 时间是 Python。它是如何工作的?

标签 python syntax timer timeit

我想为一个函数计时,我想使用 timeit 库。我在网上找不到任何好的例子。我必须对 maxcut 库中的函数“largest_eigenvector”进行计时,该函数将图形 G 作为输入,由 networkx 库中的函数返回。

所以我想为这段代码计时:

import maxcut as mc 
import networkx as nx 
G = nx.complete_graph(3)

mc.largest_eigenvector(G)

它显然工作正常。比起计时,我这样做了:

s = """
    import maxcut as mc 
    import networkx as nx 
    G = nx.complete_graph(3)
    """
t = timeit.Timer(s, 'mc.largest_eigenvector(G)')

但是它说: UnboundLocalError:赋值前引用了局部变量“mc”

我不知道为什么。请有人帮助这只是一个语法问题,我找不到合适的文档。

最佳答案

您交换了语句和设置。以相反的方式将参数传递给 Timer()。

关于python - 时间是 Python。它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7978892/

相关文章:

python - 警告 : QT_DEVICE_PIXEL_RATIO is deprecated

c++ - Range-for 循环识别错误类型 (C2440)

Python语法: unpacking multiple variables from function return value in ternary expression gives unexpected result

java - sleep、wait、park 的 native 实现可以依赖 RTC 吗?

c# - 未处理的异常 C#

python - 无法在Python脚本上拖动文件

python - 使用 GroupBy 获取 Pandas 的平均值 - 获取 DataError : No numeric types to aggregate -

haskell - Haskell 有隐式模式匹配吗?

linux - 降低 linux 内核定时器频率

具有并发性的 Python 线程