Monday, October 25, 2010

Calculate the runtime of a program

A lot of times you will want to check how much time it took for your program to complete. The time it takes for your program to complete is called runtime. Generating runtime data can be useful in many instances, like if you want to see how efficient your algorithm is, or how much time it would take for a similar program but with larger number of operations(brute force programs, for example).

To generate runtime data in Python is quite easy:-

import time
tStart=time.time()
# the main operations of your program go here.
print("Program complete")
print("Time taken:", time.time()-tStart)

This is one way to calculate runtime in Python. Do share any alternative ways to do this in the comments section!!