2018年1月9日 星期二

Python: Get execution time

Python: Get execution time

Python Basic: Exercise-57 with Solution

Write a program to get execution time (in seconds) for a Python method.
Sample Solution:-
import time
def sum_of_n_numbers(n):
    start_time = time.time()
    s = 0
    for i in range(1,n+1):
        s = s + i
    end_time = time.time()
    return s,end_time-start_time

n = 5
print("\nTime to sum of 1 to ",n," and required time to calculate is :",sum_of_n_numbers(n))

========== RESTART: F:/Python_APSC/py-ex-basic-57.py ==========

Time to sum of 1 to  5  and required time to calculate is : (15, 0.0)
>>> 

沒有留言:

張貼留言

ASK 調變模擬器程式碼

ASK 調變模擬器程式碼 為什麼第二張圖要「放大 (Zoom)」? 1 kHz  的週期是 1000 us 。 如果不放大,第二張圖會因為波形太密而看起來像一塊實心的紅磚。 import tkinter as tk from tkinter import ttk import n...