
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Execution Time of a Python Program
Python provides different ways to find the execution time taken by a script or specific parts of the code such as using the functions from the time module, like time.time() or time.clock().
The following are some common methods used to measure execution time in Python:
Getting Program Execution Time Using time.time() Function
The time.time() function returns the current time as a floating-point number that indicates the seconds elapsed since the epoch (when time began).
To calculate the execution time of a program or a piece of code, call this method before and after the code segment you want to measure. Store the returned values in variables, then subtract the initial time from the later time to get the execution duration.
Example
The following program measures the execution time of the print("Hello") statement. It records the time before and after the print statement and then calculates and displays the difference (representing the elapsed time):
import time t0 = time.time() print("Hello") t1 = time.time() print("Time elapsed: ", t1 - t0)
Following is the output of the above code:
Hello Time elapsed: 0.00002 (approx)
Getting Program Execution Time Using time.process_time() Function
The time.process_time() function measures the CPU time used by the current process. Unlike time.time(), it excludes time spent waiting for I/O operations or sleeping.
Follow the same approach as discussed above to get the start and end times to calculate the execution time of a program or code segment.
Example
The following program measures CPU execution time using time.process_time(). It records the start time, calculates the sum of squares from 0 to 99,999, then records the end time. The difference shows the CPU time spent on computation only:
import time start = time.process_time() sum([x*x for x in range(100000)]) end = time.process_time() print("CPU Time:", end - start)
Following is the output of the above code ?
CPU Time: 0.0156 (approx)
Getting Program Execution Time Using timeit Module
For more accurate measurement of a program's execution time (especially for small code snippets), you can use Python's timeit module. It runs the code multiple times and returns the shortest execution time.
Example
Here, we are defining a function print_table(n) that prints the multiplication table of a given number up to 10.
In the following example, we are using timeit.repeat() to measure the execution time to run this function in a loop. It executes the code 10,000 times and returns a list of execution times for each run.
def print_table(n): for i in range(1, 11): _ = n * i # Perform calculation without printing import timeit result = timeit.repeat("for _ in range(100): print_table(7)", "from __main__ import print_table", number=10000) print(result)
Following is the output of the above code:
[0.16064966702833772, 0.16311204596422613, 0.1629628660157323, 0.16299295308999717, 0.16286134580150247]