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