Python 中的 Timeit 及其示例?
Python 提供了許多方法來衡量一段 Python 程式碼的執行時間。一種方法是使用 Python 內建的 `time` 模組,並在程式執行之前和之後儲存時間。
Python timeit
當某個程式執行時,許多程序也在後臺執行以使該程式碼可執行。`time` 模組不計算後臺程序的執行時間,但是如果您需要精確的時間效能測量,`timeit` 是一個不錯的選擇。
`timeit` 模組執行程式碼大約 100 萬次(預設值),並考慮執行該程式碼所需的最短時間。
使用 timeit 獲取 Python 執行時間
我們可以透過多種方式使用 `timeit` 模組。最簡單的方法之一是直接在 Python CLI 上使用。
示例
我們首先將使用 `timeit` 模組在 Python CLI 上進行操作。當使用 CLI 時,我們會注意到模組本身會決定對同一程式碼段執行的重複次數。
示例 1
C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))" 1000 loops, best of 3: 290 usec per loop C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))" 1000 loops, best of 3: 292 usec per loop C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))" 1000 loops, best of 3: 294 usec per loop
示例 2
接下來,我們將介紹另一個簡單的 `timeit` 示例,但首先我們必須使用 `import timeit` 語句匯入 `timeit` 模組。如果我們不使用像上面那樣的命令列語法,這是必需的。
#Import timeit module
import timeit
# The instructions being timed.
print('x' * 5)
print('x' + 'x' + 'x' + 'x' + 'x')
# Call timeit on the statements and print the time returned.
# ... Specify optional number of iterations.
print(timeit.timeit("y = 'x' * 3", number=10000000))
print(timeit.timeit("xy = 'x' + 'x' + 'x' + 'x' + 'x'", number = 10000000))我們在上面將語句作為帶引號的字串傳遞給 `timeit.timeit` 方法,然後透過指定一個數字引數來增加迭代次數。
輸出
第一次執行上述程式時,生成的輸出
xxxxx xxxxx 0.9041136896626635 0.7712796073957123
第二次執行上述程式時,生成的輸出
xxxxx xxxxx 0.7317015874427751 0.7312688195585995
第三次執行上述程式時,生成的輸出
xxxxx xxxxx 0.7240862411172824 0.7255863890794246
我們多次(3 次)運行了上述程式,並看到執行時間有所減少。與其手動進行,不如透過程式進行重複。
#Import timeit module
import timeit
# Call timeit on the statements and print the time returned.
# ... Specify optional number of iterations.
print(timeit.repeat("y = 'x' * 3", number=10000000, repeat = 5))
print()
print(timeit.repeat("xy= 'x' + 'x' + 'x' + 'x' + 'x'", number = 10000000, repeat = 5))輸出
[0.7303736343436382, 0.7213687552991258, 0.7362311105941466, 0.7293136666273243, 0.7278277732068212] [0.7388334197158559, 0.7378481457977326, 0.9486733868277772, 0.735295442480929, 0.7398226849056382]
使用 timeit 模組執行多個語句
我們可以使用 `timeit` 模組使用多個語句。我們用分號分隔每個語句。雖然這不是編寫程式碼的最佳方法,但它有助於指定更長的程式碼片段。
#Import timeit module
import timeit
# Use semicolon for multiple statements.
print(timeit.repeat("x = 2; x *= 2", number=100000000))
print(timeit.repeat("x = 1; x *= 4", number=100000000))輸出
[24.859605879029118, 23.58795536845994, 23.95826726353284] [22.70639977603264, 21.380195994245724, 20.71523588130414]
在 Timeit 模組中使用方法和 setup
我們可以透過指定 `setup` 引數在 `timeit` 中使用自定義方法。在這個引數中,我們指定一個匯入語句,指示我們呼叫的方法。
#Import timeit module
import timeit
def func1():
return 1
def func2():
return sum([-1, 0, 1, 1])
# Test methods.
print(func1())
print(func2())
# Pass setup argument to call methods.
print(timeit.repeat("func1()", setup="from __main__ import func1"))
print(timeit.repeat("func2()", setup="from __main__ import func2"))在上面的程式中,我們將 `func1()` 方法與 `func2()` 方法進行基準測試。
輸出
1 1 [0.44798489246658874, 0.4411512652046069, 0.44570416580426686] [1.583622557983199, 1.5712399227517881, 1.5469479030713984]
由於 `func1()` 做的工作較少,因此執行速度更快。
總結
我們在上面看到了如何使用 `timeit` 模組透過 CLI 和指令碼測量小段 Python 程式碼的效能。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP