• 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
  • VBA
  • 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.  Using Python to Generate Random String of Specific Length
  • 2.  pandas median – Find Median of Series or Columns in DataFrame
  • 3.  How to Check if a Dictionary is Empty in Python
  • 4.  Get Last Character in String in Python
  • 5.  Get List of Letters in Alphabet with Python
  • 6.  How to Hide Turtle in Python with hideturtle() Function
  • 7.  Check if Number is Larger than N in Python
  • 8.  Count Number of Files in Directory with Python
  • 9.  Python Round to Nearest 10 With round() Function
  • 10.  Python Square Root Without Math Module – ** or Newton’s Method

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy