如何在Python中編寫函式來獲取每個函式的執行時間?


在本文中,我們將向您展示如何使用python編寫一個函式來獲取每個函式的執行時間。現在我們來看四種完成此任務的方法:

現在我們來看兩種完成此任務的方法:

  • 使用time.clock()函式

  • 使用time.time()函式

  • 使用time.process_time()函式

  • 使用datetime.now()函式

方法一:使用time.clock()

Python的Time模組提供了各種與時間相關的函式。

方法time.clock()在Unix系統上以秒為單位返回當前處理器時間,這是一個浮點數。精度取決於同名C函式的精度,但在任何情況下,此函式都是用於基準測試Python或計時演算法的函式。

Windows上,此函式基於Win32函式QueryPerformanceCounter,返回自第一次呼叫此函式以來經過的掛鐘秒數,這是一個浮點數。

語法

time.clock() 

引數

  • 返回值 - 返回一個浮點數,表示當前處理器時間(以秒為單位)。

  • 演算法(步驟)

    以下是執行所需任務的演算法/步驟:

    • 使用import關鍵字匯入time模組。

    • 使用clock()方法(Python 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
    

    方法二:使用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
    

    方法三:使用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()函式檢查了執行該程式碼的執行時間。

    方法四:使用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秒內執行。

    結論

    在本文中,我們學習瞭如何使用四種不同的方法來獲取程式碼的執行時間。我們使用了兩種型別的示例來演示兩個不同程式碼的執行時間之間的區別。

更新於:2022年9月22日

2K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.