python程序执行时间
The execution time of a program is defined as the time spent by the system to execute the task. As we all know any program takes some execution time but we don't know how much. So, don't worry, in this tutorial we will learn it by using the datetime module and also we will see the execution time for finding the factorial of a large number. A large number will be provided by the user and we have to calculate the factorial of a number, also we have to find the execution time of the factorial program. Before going to write the Python program, we will try to understand the algorithm.
程序的执行时间定义为系统执行任务所花费的时间。 众所周知,任何程序都需要一些执行时间,但我们不知道需要多少时间。 因此,不用担心,在本教程中,我们将通过使用datetime模块来学习它,并且还将看到查找大量因数的执行时间。 用户将提供大量的数字,我们必须计算数字的阶乘,也必须找到阶乘程序的执行时间 。 在编写Python程序之前,我们将尝试了解该算法。
Algorithm to find the execution time of a factorial program:
查找阶乘程序的执行时间的算法:
Initially, we will import the datetime module and also the math module(to find the factorial) in the Program. Take the value of a number N from the user. 最初,我们将在程序中导入datetime模块和math模块(以找到阶乘)。 从用户处获取数字N的值。 Take the value of a number N from the user. 从用户处获取数字N的值。 Find the initial time by using now() function and assign it to a variable which is t_start. 使用now()函数查找初始时间,并将其分配给t_start变量。 Calculate the factorial of a given number(N) and print it. 计算给定数字的阶乘并打印。 Here, we will also find the current time and assign it to a variable which is t_end. 在这里,我们还将找到当前时间,并将其分配给t_end变量。 To know the execution time simply find the difference between the t_end and t_start i.e t_end - t_start. 要知道执行时间只需找到t_end和t_start即t_end之间的区别- t_start。
Now, let's start writing the Python program by simply implementing the above algorithm.
现在,让我们开始通过简单地实现上述算法来编写Python程序。
# importing the modules
from datetime import datetime
import math
N=int(input("Enter the value of N: "))
t_start=datetime.now()
s=math.factorial(N)
print("factorial of the number:",s)
t_end=datetime.now()
e=t_end-t_start
print("The execution time for factorial program: ",e)
Output
输出量
Enter the value of N: 25
factorial of the number: 15511210043330985984000000
The execution time for factorial program: 0:00:00.000022
The output format of the execution time of factorial as "hours: minutes: seconds. microseconds".
阶乘执行时间的输出格式为“小时:分钟:秒。微秒” 。
翻译自: https://www.includehelp.com/python/find-the-execution-time-of-a-program.aspx
python程序执行时间
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有