• 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 / Get Elapsed Time in Seconds in Python

Get Elapsed Time in Seconds in Python

March 10, 2022 Leave a Comment

To measure the elapsed time of a process in Python, use the time module to find the starting time and ending time, and then 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 benchmark and calculate the elapsed time of a program can be very useful.

You can easily calculate the elapsed 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 time elapsed.

Below is a simple example in Python of how to get the elapsed time 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 Elapsed Time of Program in Python

When subtracting two times in Python, we get the time elapsed in seconds. However, sometimes we want 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 elapsed.

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

Below is how to convert the time elapsed to a timedelta object 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 time elapsed in a Python program.

Other Articles You'll Also Like:

  • 1.  pandas ceil – Find the Ceiling of a Column Using Numpy ceil
  • 2.  Get Username in Python using os module
  • 3.  Using Python to Convert Timestamp to Date
  • 4.  Pascal’s Triangle in Python
  • 5.  Euclidean Algorithm and Extended Euclidean Algorithm in Python
  • 6.  Scroll Up Using Selenium in Python
  • 7.  Python not in – Check if Value is Not Included in Object
  • 8.  Replace Values in Dictionary in Python
  • 9.  Python asin – Find Arcsine and Inverse Sine of Number Using math.asin()
  • 10.  Python Prepend to List with insert() Function

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