How to measure the time it takes to execute a Python file

Suppose you have some big data files to analyse, and you would like to know roughly how long it takes to run the analysis. This is so that in future, if you had to re-run the analysis, you could gauge that it will take long enough to go get a coffee or work on another job while you wait for the analysis to finish. How might you measure the time it takes to analyse the data?

If your analysis is contained in a Python script (that may import code from other libraries or custom-written Python files), one simple way to measure the time it takes to analyse files is to use Python’s datetime function at the start and end of the script:

import numpy as np
from datetime import datetime

startTime = datetime.now()

# Run analysis. For example:
np.arange(0, 2000, 1/2000)

print('\nTime elasped: ', datetime.now() - startTime)

In the short example above, Python notes the start time (in real time) just before the analysis is run, then calculates and prints the time taken to run the analysis at the end:

Time elasped:  0:00:00.014490

Here, the elapsed time is calculated in real time, so the amount of time taken to run the analysis depends on how powerful your computer’s processor is, amount of random access memory, other jobs your computer is doing, etc. This simple method is handy to get a rough estimate of time taken to run the analysis. Users who need more accurate estimates of time may consider time.clock, as well as the timeit module for small code snippits.

References

Measuring elapsed time with the Time module. Stack Overflow.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s