How to Measure Elapsed Time in Terminal using Python

In this tutorial, we’ll learn how to program "How to Measure Elapsed Time in Terminal using Python." The objective is to safely and efficiently measure and display the elapsed time of a program. This tutorial will guide you through the process step by step, ensuring all necessary elements for calculating elapsed time are handled properly. So, let’s get started!

This topic is straightforward to understand. Just follow the instructions I provide, and you’ll complete it with ease. The program I’ll demonstrate will show you the proper way to measure and display elapsed time. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import time
  2.  
  3. while True:
  4.     print("\n================= Measure Elapsed Time =================\n\n")
  5.  
  6.  
  7.     print("Starting the timer...")
  8.     print("Hellow World!")
  9.     start_time = time.time()
  10.  
  11.  
  12.     for i in range(1000000):
  13.         pass
  14.  
  15.     end_time = time.time()
  16.     elapsed_time = end_time - start_time
  17.  
  18.     print(f"Elapsed time: {elapsed_time:.4f} seconds")
  19.  
  20.     opt = input("\nDo you want to try again?(yes/no): ")
  21.  
  22.     if opt.lower() == 'yes':
  23.         ret=False
  24.     elif opt.lower() == 'no':
  25.         ret=True
  26.         print("Exiting program....")
  27.     else:
  28.         print("Please enter yes/no:")
  29.         break
  30.  
  31.     if ret == False:
  32.         continue

This program measures the time taken to execute a task (like a loop) by recording the start and end times using Python's time module, calculating the elapsed time, and displaying the result in seconds, with an option to repeat or exit.

Output:

There you have it we successfully created How to Measure Elapsed Time in Terminal using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment