GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Calculate the time runs of a program - w3resource

Python: Calculate the time runs of a program

Last update on May 17 2025 05:43:08 (UTC/GMT +8 hours)


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.


For more Practice: Solve these Related Problems:

  • Write a Python program to measure the runtime of a function with high precision.
  • Write a Python program to measure execution time excluding sleep time.
  • Write a Python function to measure runtime and return execution statistics.
  • Write a Python program to benchmark multiple function executions and compare runtimes.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

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.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.