python程序执行时间_用于在Python中查找程序执行时间的程序

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:

查找阶乘程序的执行时间的算法:

  1. 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的值。

  2. Take the value of a number N from the user.

    从用户处获取数字N的值。

  3. Find the initial time by using now() function and assign it to a variable which is t_start.

    使用now()函数查找初始时间,并将其分配给t_start变量。

  4. Calculate the factorial of a given number(N) and print it.

    计算给定数字的阶乘并打印。

  5. Here, we will also find the current time and assign it to a variable which is t_end.

    在这里,我们还将找到当前时间,并将其分配给t_end变量。

  6. 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程序执行时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值