How to get the Execution Time of A Python Program


In order to get the execution time of a Python program, you can make use of the time() function from the time module.

time.time() returns the time in seconds since the epoch as a floating point number.

Documentaion: https://docs.python.org/3/library/time.html#time.time


Example:
import time

program_start_time = time.time()

def func1():
  print("func 1 called!")

def func2():
  print("func 2 called!")

def func3():
  print("func 3 called!")

func1()
func2()
func3()

program_end_time = time.time()

print("The Program took ", program_end_time - program_start_time," seconds to run!")
Result:
func 1 called!
func 2 called!
func 3 called!
The program took  0.0028166770935058594  seconds to run!
Get Execution Time of a Python Program

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap