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: Measure execution time - w3resource

Python: Measure execution time

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


Method Execution Time

Write a Python program to get the execution time of a Python method.

Sample Solution:

Python Code:

# Import the 'time' module to work with time-related functions.
import time

# Define a function named 'sum_of_n_numbers' that calculates the sum of the first 'n' natural numbers.
def sum_of_n_numbers(n):
    # Record the current time before the calculation.
    start_time = time.time()

    # Initialize a variable 's' to store the sum.
    s = 0

    # Loop through numbers from 1 to 'n' and accumulate the sum.
    for i in range(1, n + 1):
        s = s + i

    # Record the current time after the calculation.
    end_time = time.time()

    # Return both the calculated sum and the time taken for the calculation.
    return s, end_time - start_time

# Define the value of 'n' as 5.
n = 5

# Print the result, including the time taken to calculate the sum.
print("\nTime to sum of 1 to", n, "and required time to calculate is:", sum_of_n_numbers(n))

Sample Output:

Time to sum of 1 to  5  and required time to calculate is : (15, 2.384185791015625e-06) 

Explanation:

The above Python code defines a function sum_of_n_numbers() that calculates the sum of the first n positive integers, and returns the sum and the time it took to calculate the sum.

time module - This module provides various time-related functions.

The sum_of_n_numbers() function uses a for loop to add up the first n positive integers and calculates the time it takes to do so using the time.time() function to get the start and end times of the operation. The function returns a tuple containing the sum of the first n positive integers and the time taken to calculate the sum.

Finally the code then calls the sum_of_n_numbers() function with an argument of n=5, and prints the result to the console using the print() function. The output displays the time it took to calculate the sum of the first 5 positive integers and the calculated sum.

Flowchart:

Flowchart: Get execution time.


For more Practice: Solve these Related Problems:

  • Write a Python program to measure the execution time of a function using both `time` and `timeit` modules.
  • Write a Python program to compare the execution times of iterative and recursive implementations of factorial computation.
  • Write a Python program to calculate the execution time of a loop running for a million iterations.
  • Write a Python program to measure the execution time of reading a large file line by line.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to get height and the width of console window.
Next: Write a Python program to sum of the first n positive integers.

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.