Python Benchmarking

Benchmarking is the process to measure the code execution time. Or we can measure time for a whole process which includes a set of scripts. To measure the execution time, we get the current time before the code execution starts and just after the code execution ends. Difference between the both times will be the actual execution time.

In Python, we can get current timestamp by importing “time” module.

import time

In function definition, get the current timestamp before and just after the code. Difference between both timestamp will show the actual time taken in the execution of the code. You can log the timestamp or can print it in the console.

Get the current timestamp

time() function is used to get the current timestamp from time module.

#timestamp before code execution starts
startTime = time.time()

--------
---
---    code stuff
---
---------

#timestamp just after the code execution ends
endTime = time.time()

print("----- Start Benchmark: execution time ---------")
print(endTime - startTime)
print("----- End Benchmark: execution time ---------")

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top
Shares