numpy.testing.measure#

testing.measure(code_str, times=1, label=None)[源码]#

返回执行调用者命名空间中代码所花费的时间。

提供的代码字符串使用 Python 内置的 compile 进行编译。计时精度为 10 毫秒。如果代码在此时间尺度上执行速度很快,则可以多次执行以获得合理的计时精度。

参数:
code_strstr

要计时的代码。

timesint, optional

代码执行的次数。默认为 1。代码仅编译一次。

labelstr, optional

用于标识 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