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

Print Time Elapsed in Python

February 16, 2022 Leave a Comment

In Python, the easiest way to print the time a program takes is with the Python time module. Below shows how to print time elapsed using Python.

import time

start = time.time()
for i in range(100):
    x = 1

end = time.time()

elapsed_time = end-start

print("elapsed time in seconds: " + elapsed_time )

#Output:
elapsed time in seconds: 0.00024127960205078125

When working with Python programs, the ability to know exactly how long your program takes, or how long certain sections of your program takes, can be very beneficial.

We can easily print time elapsed of a program using the Python time module.

The time module time() function gets the current time. We can get the current time before a block of code and get the current time after a block of code, and then subtract the two times to get the elapsed time.

Below is how to get the elapsed time of a Python program and print it.

import time

start = time.time()
for i in range(0,100000):
    x = 0

end = time.time()

elapsed_time = end-start

print(elapsed_time)

#Output:
0.015827178955078125

Formatting the Time Elapsed in Python

When working with times in Python, it can be useful to format them to be able to understand exactly the time that has passed in a program.

We can format the time elapsed of a program in Python easily with the timedelta() function from the Python datetime module.

For example, if we want to format the time elapsed as “hh:mm:ss” and print it to the console, we can do so as shown in the following Python code.

import time
from datetime import timedelta

start = time.time()
for i in range(0,1000000):
    x = 0

end = time.time()

elapsed_time = end-start

print("elapsed time: " + str(timedelta(seconds=elapsed_time)))

#Output:
elapsed time: 0:00:00.031208

Hopefully this article has been helpful for you to learn how to calculate and print time elapsed in Python.

Other Articles You'll Also Like:

  • 1.  Get All Substrings of a String in Python
  • 2.  Convert Integer to Bytes in Python
  • 3.  Using Lambda Expression with max() in Python
  • 4.  How to Check if List is Empty in Python
  • 5.  Python issubset() Function – Check if Set is Subset of Another Set
  • 6.  Get pandas Index Values as List in Python
  • 7.  Using readlines() and strip() to Remove Spaces and \n from File in Python
  • 8.  Get Quarter from Date in pandas DataFrame
  • 9.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 10.  Get Month Name from Date in 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