如何在Python中編寫函式來獲取每個函式的執行時間?
在這篇文章中,我們將向您展示如何使用python編寫一個函式來獲取每個函式的執行時間。現在我們看到4種方法可以完成此任務:
現在我們看到2種方法可以完成此任務:
使用time.clock()函式
使用time.time()函式
使用time.process_time()函式
使用datetime.now()函式
方法1:使用time.clock()
Python的Time模組提供了各種與時間相關的函式。
方法time.clock()在Unix系統上返回以秒為單位表示的當前處理器時間,這是一個浮點數。精度取決於同名C函式的精度,但在任何情況下,此函式都是用於基準測試Python或計時演算法的函式。
在Windows上,此函式返回自第一次呼叫此函式以來經過的掛鐘秒數,這是一個浮點數,基於Win32函式QueryPerformanceCounter。
語法
time.clock()
引數
- 返回值 - 返回一個包含以秒為單位的當前處理器時間的浮點數。
使用import關鍵字匯入time模組。
使用clock()方法獲取程式碼執行前的當前時間(Pythom time方法clock()在Unix系統上返回以秒為單位表示的當前處理器時間,這是一個浮點數),並將此儲存在一個變數中,這將是程式碼執行的開始時間。
編寫一些示例程式碼。
再次獲取當前時間。這是程式碼執行後的時間,它在程式碼執行完成後儲存為結束時間,並儲存在一個變數中。
用結束時間減去開始時間以獲取程式碼的經過時間並打印出來。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
示例
以下程式使用time.clock()函式返回程式碼執行所花費的時間:
# importing time module import time # Get the current time before executing the code starttime = time.process_time() # code print("Hello tutorialspoint python codes") # getting the time taken for executing the code in seconds endtime = time.process_time() # Printing the time taken for code execution print("Time Taken for code execution is:", endtime - starttime)
輸出
執行後,上述程式將生成以下輸出:
Hello tutorialspoint python codes Time Taken for code execution is: 0.00230999999999959
方法2:使用time.time()函式
演算法(步驟)
按照與先前方法相同的步驟操作,但使用time()函式(以秒為單位返回自time模組紀元以來(UTC)的時間,這是一個浮點數)來獲取時間。
示例
以下程式使用time.time()函式返回程式碼執行所花費的時間:
# importing time module import time # Get the current time before executing the code starttime = time.time() # code print("Hello tutorialspoint python codes") # getting the time taken for executing the code in seconds endtime = time.time() # Printing the time taken for code execution print("Time taken for code execution:", endtime - starttime)
輸出
執行後,上述程式將生成以下輸出:
Hello tutorialspoint python codes Time taken for code execution: 0.019986867904663086
方法3:使用time.process_time()函式
按照與先前方法相同的步驟操作,但使用process_time()函式(它用於計算程式碼的CPU執行時間。如果您希望測量程式的CPU執行時間,請使用time.process_time()而不是time.time())來獲取時間。
示例
以下程式使用time.process_time()函式返回程式碼執行所花費的時間:
import time # Get the current time before executing the code starttime = time.process_time() # code # Taking sample list inputList = [2, 5, 9, 1, 6] # Finding maximum element of the list maxElement = max(inputList) # Printing maximum element of the list print('Maximum Element in input list: ', maxElement) # getting the time taken for executing the code endtime = time.process_time() # Printing the time taken for code execution executionTime = endtime - starttime print('CPU Execution time:', executionTime)
輸出
執行後,上述程式將生成以下輸出:
Maximum Element in input list: 9 CPU Execution time: 0.00313188600000025
在這裡,我們執行了查詢給定列表中的最大元素的操作,並使用process_time()函式檢查了執行該程式碼的經過時間。
方法4:使用datetime.now()函式
Python包含一個DateTime模組,用於處理日期和時間。DateTime是Python中的內建模組,而不是原始資料型別;我們只需要匯入上面提到的模組即可使用日期作為日期物件。
按照與先前方法相同的步驟操作,但使用datetime.now()函式(返回當前本地日期和時間)以 HH:MM:SS 格式獲取當前日期和時間。
示例
以下程式使用datetime.now()函式返回程式碼執行所花費的時間:
# importing datetime module from datetime import datetime # getting the current time in HH:MM:SS format starttime = datetime.now() # code # Taking sample list inputList = [2, 5, 9, 1, 6] # Finding maximum element of the list maxElement = max(inputList) # Printing maximum element of the list print('Maximum Element in input list: ', maxElement) # getting the time taken for executing the code endtime = datetime.now() # Printing the time taken for code execution executionTime = endtime - starttime print('Execution Time:', executionTime)
輸出
執行後,上述程式將生成以下輸出:
Maximum Element in input list: 9 Execution Time: 0:00:00.002986
它以 HH:MM:SS 格式返回經過的時間,表明程式碼在 0.002986 秒內執行。
結論
在本文中,我們學習瞭如何使用四種不同的方法來獲取程式碼的執行時間。我們使用了兩種型別的示例來演示兩個不同程式碼的經過時間之間的區別。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP