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

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

  1. 利用python提取abaqus节点坐标的脚本_用于在Abaqus中提取结点力的Python程序

    用于在Abaqus中提取结点力的Python程序 #coding=utf-8 from abaqus import * from abaqusConstants import * from odbAc ...

  2. python掷骰子_用于掷骰子的Python程序(2人骰子游戏)

    python掷骰子 Here, we will be going to design a very simple and easy game and implement it using abstra ...

  3. python绘制组织结构图_二、Python的程序组织结构

    1.顺序结构 注:计算机的流程控制:顺序结构.选择结构.循环结构 程序从上向下执行,直到结束. print('------程序开始------') print('1.把冰箱门打开') print('2 ...

  4. python非法的_在下列Python语句中非法的是________。

    [判断题]Directions: The following exercises will test your understanding of the concepts of critical th ...

  5. python示例异常处理与程序调试_笔记:Python异常处理与程序调试

    Python异常处理与程序调试 Python提供了强大的异常处理机制,通过捕获异常可以提高程序的健壮性.异常处理还具有释放对象,中止循环的运行等作用.在程序运行的过程中,如果发生了错误,可以返回事先约 ...

  6. python优化网站_[练习] 用PYTHON来优化网站中的图片

    我到公司以来,第一次加班,哇,加一晚上加一上午,现在还没下班的迹象,555,困. 对于网站中的一些关键的页面,多重缓存.静态化.程序代码优化--之外,为了提高用户打开页面的速度,图片是必须要优化的. ...

  7. python自动控制程序_巧用 python 脚本控制你的C程序

    python是一门艺术语言,除了开发桌面程序,还能够开发网络应用,数据库应用,还可以代替shell编写一些的实用脚本,本文主要讲述了如何利用 python 程序控制你的 C 程序的行为. 作为例子,本 ...

  8. python 线性回归模型_如何在Python中建立和训练线性和逻辑回归ML模型

    python 线性回归模型 Linear regression and logistic regression are two of the most popular machine learning ...

  9. python关键词提取_如何从Python格式字符串中提取关键字? - python

    我想在API中提供自动字符串格式,例如: my_api("path/to/{self.category}/{self.name}", ...) 可以替换为格式化字符串中标注的属性值 ...

最新文章

  1. Apache Camel框架入门示例
  2. Spark编程指南笔记
  3. mysql DATE_FORMAT 年月日时分秒格式化
  4. android 副mic测试,【收藏】Android Audio Framework CTS Verifier 测试方法
  5. 一场高质量的技术盛会怎样炼成?「2019中国大数据技术大会」即将来临,邀您共赴!...
  6. 关于一个选举的票数统计程序,每一位候选人的记录内容均为字符0或1,1表示此人被选中,0表示此人未被选中,若一张选票选中人数大于5个人时被认为无效的选票。
  7. new string(abc)创建了几个对象_面试题系列第2篇:new String()创建几个对象?有你不知道的...
  8. 解决Android学习之ScollView嵌套ListView和GridView问题
  9. php小炒花生米,花生(炒)的做法_花生(炒)怎么做好吃_花生(炒)的家常做法大全【美食杰】...
  10. 安装office2010常见的提示错误解决办法
  11. 街头霸王5显示正在登陆服务器无法进入游戏,街头霸王5打不开怎么办 无法进入游戏解决方法...
  12. 喜欢吃鱼的朋友一定要转哦
  13. 三翼鸟,用两年开启下一个十年
  14. 【论文】模型剪枝(Network Pruning)论文详细翻译
  15. 自制一个 LoRa PM2.5 监测器
  16. 三、kotlin的类和对象(二)
  17. 笨木头的Unity3d常用操作介绍
  18. 养成Flash AS代码编写好习惯
  19. 余世维:如何培养领袖性格
  20. C# 兼容操作office或wps,打开excel文件

热门文章

  1. Python 文件读写
  2. linux怎么才能算telnet成功_怎么表白才算成功呢
  3. php order by where,无合适where条件过滤时尽量选择order by后的字段以驱动表进行查询...
  4. vue打包后不使用服务器直接访问方法
  5. HTML5 拖放、交换位置
  6. FFT实现高精度乘法
  7. 从xtrabackup备份恢复单表【转】
  8. 8.动态规划(1)——字符串的编辑距离
  9. 在IIS中部署Asp.net Mvc
  10. 在熟练使用2B铅笔前,请不要打开Axure