📜  如何在Python测量经过的时间?

📅  最后修改于: 2022-05-13 01:54:53.660000             🧑  作者: Mango

如何在Python测量经过的时间?

在Python,我们有三个模块可以帮助我们找到程序的执行时间。我们将在本文中探讨其中的每一个,以及它的一些可能有助于我们执行程序的功能。我们将使用的模块是timeittimeDatetime 。我们将在本文中找出经过的时间(程序执行所花费的时间)。

使用 Timeit 模块

Python timeit模块通常用于测量小代码片段的执行时间。我们还可以使用 timeit()函数来执行具有多次执行的匿名函数。在计算执行时间期间,它会暂时关闭垃圾收集(收集使用已结束的不需要的变量并通过将它们标记为垃圾值以释放内存来清除它们的过程)。

在第一个示例中,我们将分析如何使用 timeit 模块并使用它来查找 lambda 表达式的执行时间。代码从导入时间模块开始,然后我们使用. timeit() 模块用于查找执行函数所需的时间。最后,我们将结果打印在屏幕上。

Python3
# importing the module
import timeit
  
  
# using the timeit method and lambda 
# expression to get the execution time of
# the function.
t = timeit.timeit(lambda: "print('Hello World!')")
  
# printing the execution time
print(t)


Python3
# importing the module
import timeit
  
# sample function that returns square
# of the value passed
def print_square(x):
    return (x**2)
  
# using the timeit method and lambda
# expression to get the execution time of
# the function, number defines how many
# times we execute this function
t = timeit.timeit(lambda: print_square(3), number=10)
  
# printing the execution time
print(t)


Python3
# importing the module
import timeit
  
  
# sample function that returns square
# of the value passed
def print_square(x):
    return (x**2)
  
# using the repeat method and lambda
# expression to get the execution time of
# the function, number defines how many
# times we execute this function and the
# repeat defines the number of times the
# time calculation needs to be done.
t = timeit.repeat(lambda: print_square(3), number=10, repeat=5)
  
# printing the execution time
print(t)


Python3
# importing the module
import timeit
  
  
# sample function that returns 
# square of the value passed
def print_square(x):
    return (x**2)
  
# records the time at this instant
# of the program
start = timeit.default_timer()
  
# calls the function
print_square(3)
  
# records the time at this instant 
# of the program
end = timeit.default_timer()
  
# printing the execution time by subtracting
# the time before the function from
# the time after the function
print(end-start)


Python3
# importing the module
import time
  
# sample function that returns square
# of the value passed
def print_square(x):
    return (x**2)
  
# records the time at this instant of the 
# program
start = time.perf_counter()
  
# calls the function
print_square(3)
  
# records the time at this instant of the
# program
end = time.perf_counter()
  
# printing the execution time by subtracting
# the time before the function from
# the time after the function
print(end-start)


Python3
# importing the module
import time
  
# sample function that returns square 
# of the value passed
def print_square(x):
    return (x**2)
  
# records the time in nanoseceonds at
# this instant of the program
start = time.time_ns()
  
# calls the function
print_square(3)
  
# records the time in nanoseceonds at this
# instant of the program
end = time.time_ns()
  
# printing the execution time by subtracting 
# the time before the function from
# the time after the function
print(end-start)


Python3
# importing the module
from datetime import datetime
  
# sample function that returns square 
# of the value passed
def print_square(x):
    return (x**2)
  
# records the time at this instant of
# the program in HH:MM:SS format
start = datetime.now()
  
# calls the function
print_square(3)
  
# records the time at this instant of the
# program in HH:MM:SS format
end = datetime.now()
  
# printing the execution time by subtracting
# the time before the function from
# the time after the function
print(end-start)


输出:



0.0777151

现在,我们将检查如何使用timeit.timeit()函数来获取函数的执行时间。该函数从导入模块开始,然后我们创建一个示例函数,我们希望计算其执行时间。然后我们使用timeit.timeit()函数,将函数内部的函数作为参数调用,下一个参数就是定义我们执行这个函数多少次的数字。然后我们在下一行打印经过时间或执行时间。

蟒蛇3

# importing the module
import timeit
  
# sample function that returns square
# of the value passed
def print_square(x):
    return (x**2)
  
# using the timeit method and lambda
# expression to get the execution time of
# the function, number defines how many
# times we execute this function
t = timeit.timeit(lambda: print_square(3), number=10)
  
# printing the execution time
print(t)

输出:

5.299999999999749e-06

现在,实际上为了估计程序的执行时间,我们通常不使用一次获得的值作为最终正确值,因为程序的执行可能取决于特定时刻的操作系统和硬件的可用性。因此,通常我们采用多个执行时间值,通常计算出的平均值会给我们最好的答案。为此,我们将使用 timeit.repeat() 方法而不是 timeit.timeit() ,它接受一个重复参数并省去创建循环并将值存储在数组中的麻烦。

蟒蛇3

# importing the module
import timeit
  
  
# sample function that returns square
# of the value passed
def print_square(x):
    return (x**2)
  
# using the repeat method and lambda
# expression to get the execution time of
# the function, number defines how many
# times we execute this function and the
# repeat defines the number of times the
# time calculation needs to be done.
t = timeit.repeat(lambda: print_square(3), number=10, repeat=5)
  
# printing the execution time
print(t)

输出:

另外,我们可以使用 timeit.default_timer() ,它基本上记录了调用方法时的时间。因此,我们在要计算执行时间的代码行之前和之后调用该方法,然后基本上两次时间之间的差异为我们提供结果。所以为了找到时间,我们使用 timeit.defaultTimer() 方法记录时间,然后我们在最后一行打印两次之间的差异。

蟒蛇3

# importing the module
import timeit
  
  
# sample function that returns 
# square of the value passed
def print_square(x):
    return (x**2)
  
# records the time at this instant
# of the program
start = timeit.default_timer()
  
# calls the function
print_square(3)
  
# records the time at this instant 
# of the program
end = timeit.default_timer()
  
# printing the execution time by subtracting
# the time before the function from
# the time after the function
print(end-start)

输出:

2.299999999996749e-06

使用时间模块

我们可以像上面讨论的 timeit.default_timer() 方法一样使用 time.perf_counter() 方法。它可以使用尽可能高分辨率的时钟,并为您提供最准确的结果。事实上 timeit.default_timer() 也使用 time.perf_counter() 作为它的基础。这也记录了需要计算其执行或运行时间的所需代码行前后的时间。然后我们从代码行之后的记录时间中减去行开始之前的记录时间。

蟒蛇3

# importing the module
import time
  
# sample function that returns square
# of the value passed
def print_square(x):
    return (x**2)
  
# records the time at this instant of the 
# program
start = time.perf_counter()
  
# calls the function
print_square(3)
  
# records the time at this instant of the
# program
end = time.perf_counter()
  
# printing the execution time by subtracting
# the time before the function from
# the time after the function
print(end-start)

输出:

2.299999999996749e-06

要以纳秒为单位测量一段代码的运行时间或执行时间,我们可以使用 time.time_ns()函数。这遵循与 time.perf_counter()函数相同的语法,比如记录代码行前后的时间,然后减去值,然后将它们打印到屏幕上,但它以纳秒而不是秒为单位进行记录。

蟒蛇3

# importing the module
import time
  
# sample function that returns square 
# of the value passed
def print_square(x):
    return (x**2)
  
# records the time in nanoseceonds at
# this instant of the program
start = time.time_ns()
  
# calls the function
print_square(3)
  
# records the time in nanoseceonds at this
# instant of the program
end = time.time_ns()
  
# printing the execution time by subtracting 
# the time before the function from
# the time after the function
print(end-start)

输出:

0

使用日期时间模块

Datetime模块还可用于查找代码块中经过或花费的时间,前提是您不寻求高精度。 datetime.now() 的工作方式也与 timeit.default_timer() 或 time.perf_counter() 相同,但缺乏与那些相同的精度。它以 HH:MM:SS 格式返回结果。此函数通常用于获取当前时间,而不是计算执行时间的首选用例。但是,这些程序可能需要很长时间才能执行,例如训练 ML/AI 模型、网络抓取大型网站等。

蟒蛇3

# importing the module
from datetime import datetime
  
# sample function that returns square 
# of the value passed
def print_square(x):
    return (x**2)
  
# records the time at this instant of
# the program in HH:MM:SS format
start = datetime.now()
  
# calls the function
print_square(3)
  
# records the time at this instant of the
# program in HH:MM:SS format
end = datetime.now()
  
# printing the execution time by subtracting
# the time before the function from
# the time after the function
print(end-start)

输出:

0:00:00