Fundamentals 7 min read

Methods for Monitoring Python Code Execution Time and Memory Usage

This article introduces four practical techniques for measuring Python code performance, including the built‑in time module, Jupyter’s %%time magic, line_profiler for per‑line timing, and memory_profiler for detailed memory consumption, complete with example code and interpretation of results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Methods for Monitoring Python Code Execution Time and Memory Usage

During development, many of us wonder which parts of our Python code consume the most time and memory, and how to locate optimization opportunities. This article summarizes four methods to monitor Python code execution time and memory usage.

1. time module

The simplest way to measure execution time is to record timestamps before and after the code block using the built‑in time module.

<code>import time

start_time = time.time()
result = 5 + 2
end_time = time.time()

print('Time taken = {} sec'.format(end_time - start_time))</code>

An example comparing a for loop with a list comprehension demonstrates the time difference.

<code>import time

# for loop vs. list comprehension
list_comp_start_time = time.time()
result = [i for i in range(0, 1000000)]
list_comp_end_time = time.time()
print('Time taken for list comp = {} sec'.format(list_comp_end_time - list_comp_start_time))

result = []
for_loop_start_time = time.time()
for i in range(0, 1000000):
    result.append(i)
for_loop_end_time = time.time()
print('Time taken for for-loop = {} sec'.format(for_loop_end_time - for_loop_start_time))

list_comp_time = list_comp_end_time - list_comp_start_time
for_loop_time = for_loop_end_time - for_loop_start_time
print('Difference = {} %'.format((for_loop_time - list_comp_time) / list_comp_time * 100))
</code>

The output shows that the list comprehension is faster than the for loop.

2. %%time magic command

The %%time magic command, available in IPython/Jupyter notebooks, reports the total time taken by the entire cell.

<code>%%time

def convert_cms(cm, unit='m'):
    '''Function to convert cm to m or feet'''
    if unit == 'm':
        return cm / 100
    return cm / 30.48

convert_cms(1000)
</code>

The result displays CPU times and wall time, distinguishing the actual processing time from the elapsed real‑world time.

3. line_profiler

For per‑line timing, line_profiler provides detailed execution time for each line of a function. Install it with pip install line_profiler .

<code>import line_profiler

def convert_cms(cm, unit='m'):
    '''Function to convert cm to m or feet'''
    if unit == 'm':
        return cm / 100
    return cm / 30.48

%load_ext line_profiler
%lprun -f convert_cms convert_cms(1000, 'f')
</code>

The profiler output lists hits, total time, time per hit, and percentage of total time for each line, helping pinpoint bottlenecks.

4. memory_profiler

Similar to line_profiler , memory_profiler reports memory usage line by line. Install it with pip install memory_profiler .

<code>from conversions import convert_cms_f
import memory_profiler
%load_ext memory_profiler
%mprun -f convert_cms_f convert_cms_f(1000, 'f')
</code>

The output shows memory consumption (in MiB) for each line, allowing developers to understand memory footprints. Note that memory_profiler queries the operating system, so results may differ slightly from the Python interpreter.

Conclusion

Although Python is not known for raw execution speed, these tools— time , %%time , line_profiler , and memory_profiler —provide valuable insight into performance and memory usage, making them essential for effective optimization.

Performance ProfilingJupytertime moduleline_profilermemory_profiler
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.