如何在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 秒内执行。

    结论

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

更新于:2022年9月22日

2K+ 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.