Measure the Time it Takes to Run a Python Script

To measure the time it takes to run a Python script:

import time

start_time = time.time()

# Place your python script here

execution_time = time.time() - start_time
print("Execution time in seconds: " + str(execution_time))

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:

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 use the following approach to measure the time to run the script:

import time

start_time = time.time()

# Place your python script here

execution_time = time.time() - start_time
print("Execution time in seconds: " + str(execution_time))

For our example:

import time

import numpy as np
import pandas as pd

start_time = time.time()

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

execution_time = time.time() - start_time
print("Execution time in seconds: " + str(execution_time))

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

Execution time in seconds: 2.528555154800415

Leave a Comment