• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Measure Execution Time of Program in Python

How to Measure Execution Time of Program in Python

October 7, 2022 Leave a Comment

To measure the execution time of a program in Python, use the time module to find the starting time and ending time. After you have the starting time and ending time, subtract the two times.

import time

starting_time = time.time()

print("Process started...")
print("Process ended...")

ending_time = time.time()

print(ending_time - starting_time)

#Output:
0.0018320083618164062

When creating Python programs, the ability to easily calculate and display the execution time of a program can be very useful.

You can easily calculate the execution time of a piece of Python code with the help of the time module.

The time() function from the time module gets the current time. We can use time() to get the starting time, the ending time and then take the time difference to get the total time that our program is executing.

Below is a simple example in Python of how to get the total execution time of a block of code in seconds.

import time

starting_time = time.time()

print("Process started...")
print("Process ended...")

ending_time = time.time()

print(ending_time - starting_time)

#Output:
0.0018320083618164062

Formatting the Execution Time of Program in Python

When subtracting two times in Python, we get the total execution time in seconds. However, sometimes it can be useful to be able to format the time elapsed so it’s easier to read and understand.

We can use the timedelta() function from the datetime module to create a timedelta object which will format the time that it took our program to execute.

When printed to the console, timedelta objects print HH:MM:SS.

Below is how to convert the execution time to a timedelta object and print it to the console in Python.

import time
from datetime import timedelta

starting_time = time.time()

print("Process started...")
print("Process ended...")

ending_time = time.time()

print(timedelta(seconds=ending_time - starting_time))

#Output:
0:00:00.001832

Hopefully this article has been useful for you to learn how to measure and print the execution time of a Python program in Python.

Other Articles You'll Also Like:

  • 1.  Get Quarter from Date in pandas DataFrame
  • 2.  pandas interpolate() – Fill NaN Values with Interpolation in DataFrame
  • 3.  Python max float – What’s the Maximum Float Value in Python?
  • 4.  Selenium maximize_window() Function to Maximize Window in Python
  • 5.  Using Python to Check If a Number is a Perfect Square
  • 6.  Read Last Line of File Using Python
  • 7.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 8.  Using Python to Append Character to String Variable
  • 9.  Negate Boolean in Python with not Operator
  • 10.  How to Split a String in Half Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x