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: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap