#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
8 min read
•Question 40 of 41hard

Performance Optimization

Making Python code faster.

Performance Optimization

Profiling

code.pyPython
import cProfile
import pstats

# Profile function
cProfile.run('your_function()', 'output.prof')

# Analyze results
stats = pstats.Stats('output.prof')
stats.sort_stats('cumulative')
stats.print_stats(10)

# Line profiler
# pip install line_profiler
@profile
def slow_function():
    ...
# Run: kernprof -l script.py

Common Optimizations

code.pyPython
# Use list comprehension over loops
# Slow
result = []
for i in range(1000):
    result.append(i ** 2)

# Fast
result = [i ** 2 for i in range(1000)]

# Use generators for large data
def process_large(data):
    for item in data:
        yield transform(item)

# Local variables are faster
def fast_function():
    local_len = len  # Cache builtin
    for item in items:
        local_len(item)

# Use sets for membership testing
# Slow: O(n)
if item in large_list:
    pass

# Fast: O(1)
large_set = set(large_list)
if item in large_set:
    pass

NumPy for Numerics

code.pyPython
import numpy as np

# Slow pure Python
result = []
for i in range(1000000):
    result.append(i ** 2)

# Fast NumPy
result = np.arange(1000000) ** 2

Cython for Critical Code

code.pyPython
# slow_module.pyx
def sum_squares(int n):
    cdef int i, total = 0
    for i in range(n):
        total += i * i
    return total

Multiprocessing for CPU-Bound

code.pyPython
from multiprocessing import Pool

def cpu_intensive(n):
    return sum(i * i for i in range(n))

with Pool(4) as p:
    results = p.map(cpu_intensive, [10**6] * 4)

functools.lru_cache

code.pyPython
from functools import lru_cache

@lru_cache(maxsize=128)
def expensive_computation(n):
    # Cached after first call
    return sum(i ** 2 for i in range(n))