numpy.testing.measure#
- testing.measure(code_str, times=1, label=None)[源代码]#
返回在调用者的命名空间中执行代码所花费的时间.
提供的代码字符串使用 Python 内置的
compile
进行编译.计时精度为 10 毫秒.如果代码在此时间尺度上执行得很快,可以多次执行以获得合理的计时精度.- 参数:
- code_strstr
要计时的代码.
- timesint, 可选
代码执行的次数.默认为1.代码只编译一次.
- labelstr, 可选
用于标识 code_str 的标签.这作为第二个参数传递给 ``compile``(用于运行时错误消息).
- 返回:
- elapsedfloat
执行 code_str times 次的总耗时(秒).
示例
>>> times = 10 >>> etime = np.testing.measure('for i in range(1000): np.sqrt(i**2)', times=times) >>> print("Time for a single execution : ", etime / times, "s") Time for a single execution : 0.005 s