Fundamentals 7 min read

Measuring Execution Time and Memory Usage in Python

This article introduces four practical methods for monitoring Python code performance—using the built‑in time module, the %%time IPython magic, line_profiler for per‑line timing, and memory_profiler for detailed memory usage—complete with code examples and interpretation of results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Measuring Execution Time and Memory Usage in Python

When developing Python code, it is often useful to know which parts consume the most time and memory. This article summarizes four methods to monitor Python execution time and memory usage.

1. time module

The simplest way to measure elapsed time is to record timestamps before and after the code block using the 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 list comprehension with a for‑loop demonstrates the time difference.

<code>import time

# for loop vs. list comp
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>

2. %%time magic command

The %%time cell magic in IPython/Jupyter reports the time taken to execute the whole 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 output shows CPU times (user, sys) and wall time.

3. line_profiler

For per‑line timing, install line_profiler and use the %lprun magic.

<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 prints a table with hits, time per hit, and % time for each line.

4. memory_profiler

To track memory usage per line, install memory_profiler and use %mprun .

<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 lists memory usage (MiB) and increments for each line of the function.

Although Python is not known for high execution efficiency, these tools provide valuable insight for optimizing code in special cases.

PythonPerformance ProfilingmemoryTimingline_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.