Looking to measure the time it takes to run a Python script?

If so, you may use the time module in order to measure the time to run your 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, I’ll wrote the following Python script to:

- Generate 10,000,000 random integers (from 1 to 9999); and
- Convert 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 your 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))

Once I ran the code on my machine, I got the run time of 5.65 seconds:

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))

Now I got a slightly lower run time of 5.25 seconds:

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: