numpy.testing.measure#

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

返回在调用者的命名空间中执行代码所用的时间.

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

参数:
code_strstr

要计时的代码.

times整数,可选

代码执行的次数.默认为 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