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程序執行時間

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379319.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379319.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379319.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

公約數和公倍數

描述 小明被一個問題給難住了&#xff0c;現在需要你幫幫忙。問題是&#xff1a;給出兩個正整數&#xff0c;求出它們的最大公約數和最小公倍數。 輸入 第一行輸入一個整數n&#xff08;0< n<10000)&#xff0c;表示有n組測試數據; 隨后的n行輸入兩個整數i,j&#xff08;…

.net 筆記嘗試(二)

.NET筆試題集&#xff08;二&#xff09; 1.using關鍵字有什么用&#xff1f;什么是IDisposable&#xff1f; using可以聲明namespace的引入&#xff0c;還可以實現非托管資源的釋放&#xff0c;實現了IDisposiable的類在using中創建&#xff0c;using結束后會自動調用該對象的…

網上照片之博客照片與網店照片拍攝心得

本文選自《非常攝影手記&#xff1a;2天玩轉單反相機》一書 讓照片在博客上更熱的心得 1.有時美好的不僅是照片&#xff0c;還有拍攝照片的過程。盡量真實全面地記錄自己的攝影過程&#xff0c;展示最感人的自我。 2.不要急功近利&#xff0c;不想成“名博”的博主不是好博主&a…

string.length_C.中的String.Length屬性示例

string.lengthC&#xff03;String.Length屬性 (C# String.Length property) String.Length property is used to get the total number of characters in the string object (length of the string); it calls with this string and returns the total number of characters. …

匯編語言-014(編寫過程的應用、偽指令LEA、ENTER、LEAVE、LOCAL、遞歸函數、INVOKE、ADDR、PROC偽指令聲明過程)

1&#xff1a;編寫過程&#xff0c;僅用移位和加法&#xff0c;實現任意32位符號數與EAX相乘 include Irvine32.inc.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.code main PROC mov eax,80mov ebx,53call BitwiseMultiplycall WriteDecINVOKE ExitProcess,0 main ENDP;EA…

從鍵盤輸入一個字符,判斷其是不是大寫字母,如果是則請輸出這個大寫字母,如果不是請輸出“這不是一個大寫字母”的英文信息(要求:能連續輸出直到輸出“#”結束)。

從鍵盤輸入一個字符&#xff0c;判斷其是不是大寫字母&#xff0c;如果是則請輸出這個大寫字母&#xff0c;如果不是請輸出“這不是一個大寫字母”的英文信息&#xff08;要求&#xff1a;能連續輸出直到輸出“#”結束&#xff09;。 P155 例4.13 DATA SEGMENT STR DB 0DH,0…

再談angularjs DI(Dependency Injection)

在前面已經介紹了關于angularjs&#xff0c;以及擴展了一些jQuery ui的一些組件為angularjs的directive。在這里應進口007 在上篇留言我們來看看在angularjs中的DI特性。 DI&#xff1a;依賴注入&#xff0c;是一種軟件設計模式&#xff0c;應DIP依賴倒置原則&#xff0c;描述組…

Access sql語句創建表及字段類型

Create TABLE 測試表 ( 文本255 VARCHAR NOT NULL, 文本20 VARCHAR(20) NOT NULL, 日期時間 DATETIME, 數字1 BYTE, 數字2 SMALLINT, 數字4 INTEGER, 布爾 BIT, 自動編號 COUNTER(10, 5) CONSTRAINT PK_TVIPLevel26 PRIMARY KEY, 小數 NUME…

c語言getc函數_C語言中的getc()函數與示例

c語言getc函數C語言中的getc()函數 (getc() function in C) Prototype: 原型&#xff1a; int getc(FILE *filename);Parameters: 參數&#xff1a; FILE *filenameReturn type: int 返回類型&#xff1a; int Use of function: 使用功能&#xff1a; In the file handling…

匯編語言-015(PROC偽指令定義參數方式、EXTERNDEF、INCLUDE 、EXTERN 、INVOKE、PROC、PROTO 、MOVSB 、MOVSD 、CMPSD )

1&#xff1a;使用PROC偽指令定義參數方式不通過寄存器傳參&#xff0c;計算數組之和 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data array DWORD 10000h,20000h,30000h,40000h,50000h theSum DWORD ?.code ArraySum PROC USES esi ecx,ptr…

編寫計算表達式(X-Y+25)/Z的值得程序,要求將其商和余數分別放在A、B單元中。(設X和Y是32位無符號數,A、B和Z是16位無符號數,不考慮溢出情況。)

編寫計算表達式&#xff08;X-Y25&#xff09;/Z的值得程序&#xff0c;要求將其商和余數分別放在A、B單元中。&#xff08;設X和Y是32位無符號數&#xff0c;A、B和Z是16位無符號數&#xff0c;不考慮溢出情況。&#xff09; P151 例4.8 匯編思路&#xff1a;首先在DATA段定…

《深入淺出WPF》筆記——綁定篇(一)

上一節&#xff0c;有記錄寫到&#xff1a;在WPF里&#xff0c;數據驅動UI&#xff0c;數據占核心地位&#xff0c;UI次之。怎么恢復數據的核心地位&#xff0c;那就要先了解一下Binding。 一、Binding 基礎 1.1WPF中Data Binding的帶來的方便 在設計架構的時間&#xff0c;大家…

你的代碼完成了嗎?(之二)——可維護性和規范性

二. 可維護性和規范性 對于代碼來說&#xff0c;這兩個屬性其實是緊密相連的。什么樣的代碼最好維護呢&#xff1f;當然是規范的代碼了。再差的規范也要比沒有規范強得多。 之前做對日項目的時候&#xff0c;日本人對于“規范”這個東西&#xff08;他們稱之為開發規約&#xf…

c語言feof函數_使用示例的C語言中的feof()函數

c語言feof函數C語言中的feof()函數 (feof() function in C) Prototype: 原型&#xff1a; int feof(FILE* filename);Parameters: 參數&#xff1a; FILE *filenameReturn type: int(0 or 1) 返回類型&#xff1a; int(0或1) Use of function: 使用功能&#xff1a; In C l…

5種經典排序算法,每個程序員都應該知道

我的新書《Android App開發入門與實戰》已于2020年8月由人民郵電出版社出版&#xff0c;歡迎購買。點擊進入詳情 有沒有想過當您應用從低到高、從高到低或按字母順序等過濾器時&#xff0c;亞馬遜或任何其他電子商務網站中的產品如何排序&#xff1f;排序算法對于此類網站起著至…

匯編語言-016(SCASB 、STOSB 、LODSD 、數組中的取值、二維數組操作、冒泡排序和二分查找、CMPSW )

1&#xff1a; SCASB : (字節)將AL的值與EDI尋址的一個字比較。進行在一個字符串檢索特定的字符 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data alpha BYTE "ABCDEFGH",0.code main PROCmov edi,OFFSET alpha ;EDI指向字符串mov …

編寫一個匯編語言程序,將字符串Hello World中的全部小寫字母轉換為大寫字母,并存放回原地址處。

編寫一個匯編語言程序&#xff0c;將字符串"Hello World"中的全部小寫字母轉換為大寫字母&#xff0c;并存放回原地址處。 P176 4.10 編程思路&#xff1a;首先DATA段中&#xff0c;定義string存放字符串"Hello World"&#xff0c;LEN為字符串string長度…

在Python中使用OpenCV(CV2)對圖像進行邊緣檢測

Modules used: 使用的模塊&#xff1a; For this, we will use the opencv-python module which provides us various functions to work on images. 為此&#xff0c;我們將使用opencv-python模塊&#xff0c;該模塊為我們提供了處理圖像的各種功能。 Download opencv-pytho…

需保留小數點兩位,但同時不要小數點后多余0的前后臺代碼實現

今天碰到一個需求。關于小數點的處理&#xff0c;看起來非常簡單的事情&#xff0c;卻花了一定時間做了一些試驗。最后簡單總結一下&#xff0c;以便備忘。 需求簡化一下表達是這樣的&#xff1a; 有A、B兩列&#xff0c;A/BC。這3列在數據庫中都以decimal存放。 在應用中&…

匯編語言-017(SCASW 、STRUCT 、STRUCT_ALLPOINTS 、STRUCT_ALIGN 、SYSTEMTIME、UNION 、 STRUCTTEST )

1&#xff1a;SCASW : 在wordArray中掃描16位數值0100h,將匹配元素的偏移量復制到EAX .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data wordArray WORD 0500h,0400h,0300h,0200h,0100h.code main PROCmov ax,0100hmov edi,OFFSET wordArraymov …