如何用 Python 编写一个函数来获取每个函数所花费的时间?

pythonserver side programmingprogramming更新于 2025/5/8 13:00:17

在本文中,我们将向您展示如何使用 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() 函数(以浮点数形式返回时间,以 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 秒。

    结论

    在本文中,我们学习了如何使用四种不同的方法获取代码所花费的时间。我们使用两种类型的示例来演示两种不同代码运行时间的区别。


相关文章


有用资源