Python – Measure time taken by program to execute
This article aims to show how to measure the time taken by the program to execute. Calculating time helps to optimize your Python script to perform better.
Approach #1 :
A simple solution to it is to use time module to get the current time. The following steps calculate the running time of a program or section of a program.
- Store the starting time before the first line of the program executes.
- Store the ending time after the last line of the program executes.
- Print the difference between start time and end time.
Code #1 :
GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks Total runtime of the program is 1.0009586811065674
Approach #2 : Using Timeit module
Approach #3 : Using default_timer() method in timeit module
Approach #4 : Using the datetime module
Note: Output may vary depending on the system or server load. To read more about Timeit modulule, refer – Timeit in Python with Examples