前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python程序执行时间_用于在Python中查找程序执行时间的程序

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

作者头像
用户7886150
修改2021-01-28 10:03:45
2K0
修改2021-01-28 10:03:45
举报
文章被收录于专栏:bit哲学院

参考链接: 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: 

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

 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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档