📜  如何检查Python脚本的执行时间?

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

如何检查Python脚本的执行时间?

在本文中,我们将讨论如何测量Python脚本的运行时间。

还有像时间,timeit日期时间在Python可以存储在正被执行的程序的特定部分中的时间很多Python模块。通过操纵或获取执行特定部分的开始和结束时间之间的差异,我们可以计算执行该部分所花费的时间。

以下方法可用于计算时差:



  • Python的时间模块提供了各种与时间相关的函数。该模块属于 Python 的标准实用程序模块。 Time 模块的time.time()方法用于获取自纪元以来的时间(以秒为单位)。闰秒的处理取决于平台。
  • Python datetime 库定义了一个函数,主要用于获取当前时间和日期。 now()函数返回当前本地日期和时间,它在 datetime 模块下定义。
  • timeit运行您的代码片段数百万次(默认值为 1000000),以便您获得统计上最相关的代码执行时间度量。

示例 1:使用 time 模块和 time.time()函数计算时间

Python3
# program to compute the time
# of execution of any python code
import time
 
# we initialize the variable start
# to store the starting time of
# execution of program
start = time.time()
 
# we can take any program but for
# example we have taken the below
# program
a = 0
for i in range(1000):
    a += (i**100)
 
# now we have initialized the variable
# end to store the ending time after
# execution of program
end = time.time()
 
# difference of start and end variables
# gives the time of execution of the
# program in between
print("The time of execution of above program is :", end-start)


Python3
# program to compute the time
# of execution of any python code
# for different number of computations
import time
 
# we initialize a for loop and in each
# iterations store the time of start
# and end of the iterations
for j in range(100, 1100, 100):
    start = time.time()
 
    # program to iterate the range of
    # below loop increasing the value
    # in each iterations
    a = 0
    for i in range(j):
        a += (i**100)
 
    # the end variable to store the
    # ending time after execution of
    # program after each iterations
    end = time.time()
 
    # difference of start and end variables
    # gives the time of execution of the program
    # in between in each iterations
    print("Time for execution of program for {} order of computations: {}".format(
        j, round(end-start, 10)))


Python3
# program to compute the time of
# execution of any python code
from datetime import datetime
 
# we initialize the variable start to
# store the starting time of execution
# of program
start = datetime.now()
 
# we can take any program but for
# example we have taken the below
# program
a = 0
for i in range(1000):
    a += (i**100)
 
# now we have initialized the variable
# end to store the ending time after
# execution of program
end = datetime.now()
 
# difference of start and end variables
# gives the time of execution of the
# program in between
print("The time of execution of above program is :",
      str(end-start)[5:])


Python3
# program to compute the time of
# execution of any python code using timit
 
# importing the required module
import timeit
 
# code snippet to be executed only once
mysetup = "from math import sqrt"
 
# code snippet whose execution time
# is to be measured
mycode = '''
def example():
    mylist = []
    for x in range(100):
        mylist.append(sqrt(x))
'''
 
# timeit statement
print ("The time of execution of above program is :",
       timeit.timeit(setup = mysetup,
                    stmt = mycode,
                    number = 10000))


输出:

The time of execution of above program is : 0.001995563507080078

我们已经计算了上述程序的时间,其顺序为 10^-3。我们可以通过使用相同的算法增加计算次数来检查时间。

示例 2:检查不同计算次数的程序执行时间。

蟒蛇3



# program to compute the time
# of execution of any python code
# for different number of computations
import time
 
# we initialize a for loop and in each
# iterations store the time of start
# and end of the iterations
for j in range(100, 1100, 100):
    start = time.time()
 
    # program to iterate the range of
    # below loop increasing the value
    # in each iterations
    a = 0
    for i in range(j):
        a += (i**100)
 
    # the end variable to store the
    # ending time after execution of
    # program after each iterations
    end = time.time()
 
    # difference of start and end variables
    # gives the time of execution of the program
    # in between in each iterations
    print("Time for execution of program for {} order of computations: {}".format(
        j, round(end-start, 10)))

输出:

我们看到计算时间随着执行次数的增加而增加的总体趋势。但是,它可能不会显示任何线性趋势或固定增量。

示例 3:在Python和 datetime.now函数使用 datetime 模块。

蟒蛇3

# program to compute the time of
# execution of any python code
from datetime import datetime
 
# we initialize the variable start to
# store the starting time of execution
# of program
start = datetime.now()
 
# we can take any program but for
# example we have taken the below
# program
a = 0
for i in range(1000):
    a += (i**100)
 
# now we have initialized the variable
# end to store the ending time after
# execution of program
end = datetime.now()
 
# difference of start and end variables
# gives the time of execution of the
# program in between
print("The time of execution of above program is :",
      str(end-start)[5:])

输出:

The time of execution of above program is : 00.001996

示例 4:使用 timeit

这将为我们提供任何程序的执行时间。该模块提供了一种简单的方法来查找一小段Python代码的执行时间。它提供了timeit()方法来做同样的事情。模块函数timeit.timeit(stmt, setup, timer, number) 接受四个参数:

  • stmt 这是您要测量的语句;它默认为“通过”。
  • setup 这是您在运行 stmt 之前运行的代码;它默认为“通过”。我们通常使用它来为我们的代码导入所需的模块。
  • timer 是一个 timeit.Timer 对象;它通常有一个合理的默认值,所以你不必担心它。
  • number 是您想要运行 stmt 的执行次数。

蟒蛇3

# program to compute the time of
# execution of any python code using timit
 
# importing the required module
import timeit
 
# code snippet to be executed only once
mysetup = "from math import sqrt"
 
# code snippet whose execution time
# is to be measured
mycode = '''
def example():
    mylist = []
    for x in range(100):
        mylist.append(sqrt(x))
'''
 
# timeit statement
print ("The time of execution of above program is :",
       timeit.timeit(setup = mysetup,
                    stmt = mycode,
                    number = 10000))

输出:

The time of execution of above program is : 0.0023286999994525104