How to Measure the Time it Takes to Run Python Script

You may use the time module in order to measure the time it takes to run a Python script:

import time
startTime = time.time()

#####your python script#####

executionTime = (time.time() - startTime)
print('Execution time in seconds: ' + str(executionTime))

Next, you’ll see the steps to apply the above approach in practice.

Steps to Measure the Time it Takes to Run a Python Script

Step 1: Write the Python Script

For illustration purposes, let’s write a Python script that:

  • Generates 10,000,000 random integers (from 1 to 9999); and
  • Converts those integers to strings
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(1,9999,size=(10000000, 1)), columns=['Random numbers'])
df['Random numbers'] = df['Random numbers'].astype(str)

The ultimate goal is to measure the time it takes to run the above script.

Step 2: Measure the Time to Run the Python Script

You may now use the following approach to measure the time to run the script:

import time
startTime = time.time()

#####your python script#####

executionTime = (time.time() - startTime)
print('Execution time in seconds: ' + str(executionTime))

For our example, you may apply the following syntax in Python (make sure that both the Pandas and NumPy modules are installed first):

import time
startTime = time.time()

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(1,9999,size=(10000000, 1)), columns=['Random numbers'])
df['Random numbers'] = df['Random numbers'].astype(str)

executionTime = (time.time() - startTime)
print('Execution time in seconds: ' + str(executionTime))

Here is an example of the time it took to run the above script:

Execution time in seconds: 7.513572454452515

You may have noticed that in the above code we measured the time it took to:

  • Import both the Pandas and the NumPy modules; and
  • Run the main Python script to generate the random integers and then convert them to strings

Alternatively, you may check how long it takes to run the main Python script, excluding the time it takes to import both the Pandas and the NumPy modules:

import pandas as pd
import numpy as np
import time

startTime = time.time()

df = pd.DataFrame(np.random.randint(1,9999,size=(10000000, 1)), columns=['Random numbers'])
df['Random numbers'] = df['Random numbers'].astype(str)

executionTime = (time.time() - startTime)
print('Execution time in seconds: ' + str(executionTime))

Here you should get a slightly lower run time:

Execution time in seconds: 7.2620604038238525

Optionally, you may break down the time it takes to run each portion of the code. For example, you may want to separately measure the time it takes to:

  • Import the modules
  • Run the main Python Script
import time

startTime_1 = time.time()
import pandas as pd
import numpy as np
executionTime_1 = (time.time() - startTime_1)
print('Time to import modules: ' + str(executionTime_1))

startTime_2 = time.time()
df = pd.DataFrame(np.random.randint(1,9999,size=(10000000, 1)), columns=['Random numbers'])
df['Random numbers'] = df['Random numbers'].astype(str)
executionTime_2 = (time.time() - startTime_2)
print('Time to run the main Python script: ' + str(executionTime_2))

And here is the final result:

Time to import modules: 0.3277866840362549
Time to run the main Python script: 7.2637856006622314