w3resource

Python: Calculate the time runs of a program

Python Basic: Exercise-133 with Solution

Program Runtime Calculator

Write a Python program to calculate the time runs (difference between start and current time) of a program.

Sample Solution:

Python Code:

# Import the 'default_timer' function from the 'timeit' module to measure elapsed time.
from timeit import default_timer
# Define a function 'timer' that takes an argument 'n'.
def timer(n):
    # Record the current time using the 'default_timer' function.
    start = default_timer()
   
    # Some code here (in this case, a loop to print numbers from 0 to 'n').
    for row in range(0, n):
        print(row)
    
    # Calculate the elapsed time by subtracting the start time from the current time,
    # and print the result.
    print(default_timer() - start)

# Call the 'timer' function with different values of 'n' to measure the execution time.
timer(5)
timer(15)

Sample Output:

0                                                                                                             
1                                                                                                             
2                                                                                                             
3                                                                                                             
4                                                                                                             
2.6107000849151518e-05                                                                                        
0                                                                                                             
1                                                                                                             
2                                                                                                             
3                                                                                                             
4                                                                                                             
5                                                                                                             
6                                                                                                             
7                                                                                                             
8                                                                                                             
9                                                                                                             
10                                                                                                            
11                                                                                                            
12                                                                                                            
13                                                                                                            
14                                                                                                            
4.1371999941475224e-05

Flowchart:

Flowchart: Calculate the time runs of a program.

Python Code Editor:

 

Previous: Write a Python program to list home directory without absolute path.
Next: Write a Python program to input two integers in a single line.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/python-basic-exercise-133.php