Profiling and Measuring Execution Durations

๐Ÿท๏ธ Working with Dates and Time / The Time Module

When writing Python scripts, it's often important to know how long certain operations take to complete. Whether you're optimizing a slow function, comparing two approaches, or simply monitoring performance, measuring execution time helps you make informed decisions. This guide introduces simple, practical ways to profile and measure how long your code runs.


โš™๏ธ Why Measure Execution Time?

  • Identify bottlenecks โ€“ Find out which parts of your code are slowest.
  • Compare approaches โ€“ See if one method runs faster than another.
  • Monitor performance โ€“ Track how changes affect speed over time.
  • Set expectations โ€“ Know how long a script will take before running it.

๐Ÿ› ๏ธ Using the time Module for Simple Timing

The time module provides a straightforward way to measure durations. The most common approach is to capture the current time before and after a block of code, then calculate the difference.

Basic pattern:

  • Call time.time() before your code starts and store it in a variable like start_time.
  • Run the code you want to measure.
  • Call time.time() again after the code finishes and store it as end_time.
  • Subtract start_time from end_time to get the elapsed seconds.

Example (inline):

  • Import the time module.
  • Set start = time.time() .
  • Write a loop or function call you want to test.
  • Set end = time.time() .
  • Print f"Execution time: {end - start} seconds" .

Expected output (example): Execution time: 0.0342 seconds


๐Ÿ“Š Comparing time.time() vs time.perf_counter()

For most timing needs, time.time() works well. However, for very short operations or high-precision measurements, time.perf_counter() is better because it uses the most accurate clock available on your system.

Function Best Use Case Notes
time.time() General timing for longer operations May be affected by system clock adjustments
time.perf_counter() High-precision timing for short code Includes time during sleep, not affected by clock changes

When to use each:

  • Use time.time() for scripts that run for several seconds or more.
  • Use time.perf_counter() when measuring milliseconds or microseconds.

๐Ÿ•ต๏ธ Profiling Multiple Sections of Code

Sometimes you want to measure several parts of a script independently. A simple approach is to store timestamps at key points and calculate differences.

Example approach:

  • Record start at the beginning of the script.
  • After the first section, record checkpoint1.
  • After the second section, record checkpoint2.
  • At the end, print checkpoint1 - start, checkpoint2 - checkpoint1, and so on.

This gives you a breakdown of where time is spent without needing any external tools.


๐Ÿ“ˆ Using timeit for Repeated Measurements

The timeit module is designed specifically for timing small code snippets. It runs your code many times and returns the average duration, which helps reduce noise from system load.

How to use timeit (inline):

  • Import timeit.
  • Define your code as a string or a callable function.
  • Call timeit.timeit("your_code_here", number=1000) to run it 1000 times.
  • The result is the total time for all runs. Divide by number to get the average per run.

Example (inline):

  • Set code_to_test = "sum(range(100))" .
  • Call timeit.timeit(code_to_test, number=10000) .
  • Print the result.

Expected output (example): 0.0458 seconds total for 10000 runs, or about 0.00000458 seconds per run.


๐Ÿงช Practical Tips for Accurate Timing

  • Run multiple times โ€“ A single measurement can be misleading. Run your code several times and take the average.
  • Avoid including setup code โ€“ Only time the part you care about, not imports or variable assignments.
  • Close other programs โ€“ Background processes can affect timing results.
  • Use larger workloads โ€“ For very fast code, increase the input size or repeat count to get measurable durations.
  • Be consistent โ€“ Always use the same timing function (e.g., perf_counter) when comparing results.

โœ… Summary

  • Use time.time() or time.perf_counter() to measure execution duration in your scripts.
  • Store start and end timestamps, then subtract to get elapsed time.
  • For high-precision needs, prefer time.perf_counter() .
  • Use timeit for reliable timing of small code snippets by running them many times.
  • Profile multiple sections by recording checkpoints throughout your script.

Measuring execution time is a simple but powerful skill that helps you write faster, more efficient code. Start with the basic pattern and expand as your needs grow.


This topic shows how to measure how long your Python code takes to run, helping engineers find slow parts in their programs.


โฑ๏ธ Example 1: Measuring a single code block with time.time()

This example captures the current time before and after a task, then subtracts to find the duration.

import time

start = time.time()
total = 0
for i in range(1000000):
    total = total + i
end = time.time()

duration = end - start

๐Ÿ“ค Output: 0.0453 (value will vary on your machine)


โฑ๏ธ Example 2: Using time.perf_counter() for higher precision

This example uses a more precise timer than time.time(), which is better for short code snippets.

import time

start = time.perf_counter()
result = 1
for i in range(1, 1000):
    result = result * i
end = time.perf_counter()

duration = end - start

๐Ÿ“ค Output: 0.000312 (value will vary on your machine)


โฑ๏ธ Example 3: Wrapping a function call with timing logic

This example measures how long it takes to call a function that simulates a slow operation.

import time

def slow_calculation():
    total = 0
    for i in range(500000):
        total = total + (i * i)
    return total

start = time.perf_counter()
result = slow_calculation()
end = time.perf_counter()

duration = end - start

๐Ÿ“ค Output: 0.0221 (value will vary on your machine)


โฑ๏ธ Example 4: Comparing two different approaches to the same task

This example measures two ways of building a string to see which is faster.

import time

start = time.perf_counter()
text = ""
for i in range(10000):
    text = text + str(i)
end = time.perf_counter()
method1_time = end - start

start = time.perf_counter()
parts = []
for i in range(10000):
    parts.append(str(i))
text = "".join(parts)
end = time.perf_counter()
method2_time = end - start

๐Ÿ“ค Output: method1_time: 0.0089, method2_time: 0.0012 (values will vary)


โฑ๏ธ Example 5: Running multiple trials and averaging the result

This example runs the same code five times and reports the average duration for more reliable measurements.

import time

def test_function():
    total = 0
    for i in range(200000):
        total = total + (i * 3)
    return total

num_trials = 5
total_time = 0

for trial in range(num_trials):
    start = time.perf_counter()
    result = test_function()
    end = time.perf_counter()
    total_time = total_time + (end - start)

average_time = total_time / num_trials

๐Ÿ“ค Output: 0.0089 (value will vary on your machine)


Comparison Table

Function Best Use Case Precision Notes
time.time() Long-running tasks (> 0.1 sec) Seconds (low) Affected by system clock adjustments
time.perf_counter() Short code snippets Nanoseconds (high) Includes sleep time, best for profiling

When writing Python scripts, it's often important to know how long certain operations take to complete. Whether you're optimizing a slow function, comparing two approaches, or simply monitoring performance, measuring execution time helps you make informed decisions. This guide introduces simple, practical ways to profile and measure how long your code runs.


โš™๏ธ Why Measure Execution Time?

  • Identify bottlenecks โ€“ Find out which parts of your code are slowest.
  • Compare approaches โ€“ See if one method runs faster than another.
  • Monitor performance โ€“ Track how changes affect speed over time.
  • Set expectations โ€“ Know how long a script will take before running it.

๐Ÿ› ๏ธ Using the time Module for Simple Timing

The time module provides a straightforward way to measure durations. The most common approach is to capture the current time before and after a block of code, then calculate the difference.

Basic pattern:

  • Call time.time() before your code starts and store it in a variable like start_time.
  • Run the code you want to measure.
  • Call time.time() again after the code finishes and store it as end_time.
  • Subtract start_time from end_time to get the elapsed seconds.

Example (inline):

  • Import the time module.
  • Set start = time.time() .
  • Write a loop or function call you want to test.
  • Set end = time.time() .
  • Print f"Execution time: {end - start} seconds" .

Expected output (example): Execution time: 0.0342 seconds


๐Ÿ“Š Comparing time.time() vs time.perf_counter()

For most timing needs, time.time() works well. However, for very short operations or high-precision measurements, time.perf_counter() is better because it uses the most accurate clock available on your system.

Function Best Use Case Notes
time.time() General timing for longer operations May be affected by system clock adjustments
time.perf_counter() High-precision timing for short code Includes time during sleep, not affected by clock changes

When to use each:

  • Use time.time() for scripts that run for several seconds or more.
  • Use time.perf_counter() when measuring milliseconds or microseconds.

๐Ÿ•ต๏ธ Profiling Multiple Sections of Code

Sometimes you want to measure several parts of a script independently. A simple approach is to store timestamps at key points and calculate differences.

Example approach:

  • Record start at the beginning of the script.
  • After the first section, record checkpoint1.
  • After the second section, record checkpoint2.
  • At the end, print checkpoint1 - start, checkpoint2 - checkpoint1, and so on.

This gives you a breakdown of where time is spent without needing any external tools.


๐Ÿ“ˆ Using timeit for Repeated Measurements

The timeit module is designed specifically for timing small code snippets. It runs your code many times and returns the average duration, which helps reduce noise from system load.

How to use timeit (inline):

  • Import timeit.
  • Define your code as a string or a callable function.
  • Call timeit.timeit("your_code_here", number=1000) to run it 1000 times.
  • The result is the total time for all runs. Divide by number to get the average per run.

Example (inline):

  • Set code_to_test = "sum(range(100))" .
  • Call timeit.timeit(code_to_test, number=10000) .
  • Print the result.

Expected output (example): 0.0458 seconds total for 10000 runs, or about 0.00000458 seconds per run.


๐Ÿงช Practical Tips for Accurate Timing

  • Run multiple times โ€“ A single measurement can be misleading. Run your code several times and take the average.
  • Avoid including setup code โ€“ Only time the part you care about, not imports or variable assignments.
  • Close other programs โ€“ Background processes can affect timing results.
  • Use larger workloads โ€“ For very fast code, increase the input size or repeat count to get measurable durations.
  • Be consistent โ€“ Always use the same timing function (e.g., perf_counter) when comparing results.

โœ… Summary

  • Use time.time() or time.perf_counter() to measure execution duration in your scripts.
  • Store start and end timestamps, then subtract to get elapsed time.
  • For high-precision needs, prefer time.perf_counter() .
  • Use timeit for reliable timing of small code snippets by running them many times.
  • Profile multiple sections by recording checkpoints throughout your script.

Measuring execution time is a simple but powerful skill that helps you write faster, more efficient code. Start with the basic pattern and expand as your needs grow.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

This topic shows how to measure how long your Python code takes to run, helping engineers find slow parts in their programs.


โฑ๏ธ Example 1: Measuring a single code block with time.time()

This example captures the current time before and after a task, then subtracts to find the duration.

import time

start = time.time()
total = 0
for i in range(1000000):
    total = total + i
end = time.time()

duration = end - start

๐Ÿ“ค Output: 0.0453 (value will vary on your machine)


โฑ๏ธ Example 2: Using time.perf_counter() for higher precision

This example uses a more precise timer than time.time(), which is better for short code snippets.

import time

start = time.perf_counter()
result = 1
for i in range(1, 1000):
    result = result * i
end = time.perf_counter()

duration = end - start

๐Ÿ“ค Output: 0.000312 (value will vary on your machine)


โฑ๏ธ Example 3: Wrapping a function call with timing logic

This example measures how long it takes to call a function that simulates a slow operation.

import time

def slow_calculation():
    total = 0
    for i in range(500000):
        total = total + (i * i)
    return total

start = time.perf_counter()
result = slow_calculation()
end = time.perf_counter()

duration = end - start

๐Ÿ“ค Output: 0.0221 (value will vary on your machine)


โฑ๏ธ Example 4: Comparing two different approaches to the same task

This example measures two ways of building a string to see which is faster.

import time

start = time.perf_counter()
text = ""
for i in range(10000):
    text = text + str(i)
end = time.perf_counter()
method1_time = end - start

start = time.perf_counter()
parts = []
for i in range(10000):
    parts.append(str(i))
text = "".join(parts)
end = time.perf_counter()
method2_time = end - start

๐Ÿ“ค Output: method1_time: 0.0089, method2_time: 0.0012 (values will vary)


โฑ๏ธ Example 5: Running multiple trials and averaging the result

This example runs the same code five times and reports the average duration for more reliable measurements.

import time

def test_function():
    total = 0
    for i in range(200000):
        total = total + (i * 3)
    return total

num_trials = 5
total_time = 0

for trial in range(num_trials):
    start = time.perf_counter()
    result = test_function()
    end = time.perf_counter()
    total_time = total_time + (end - start)

average_time = total_time / num_trials

๐Ÿ“ค Output: 0.0089 (value will vary on your machine)


Comparison Table

Function Best Use Case Precision Notes
time.time() Long-running tasks (> 0.1 sec) Seconds (low) Affected by system clock adjustments
time.perf_counter() Short code snippets Nanoseconds (high) Includes sleep time, best for profiling