Six Techniques to Improve Python Code Performance
This article explains why Python may run slower than compiled languages, introduces methods for detecting performance bottlenecks, and presents six practical techniques—including using timeit, memory_profiler, line_profiler, built‑in functions, f‑strings, list comprehensions, and lru_cache—to significantly speed up Python programs.
Many developers complain that Python is slow, but execution speed largely depends on the programmer’s algorithms and code optimization. While Python’s development speed is high, its runtime performance can be improved with specific techniques.
Code Performance Detection
Before optimizing, identify which code sections slow down the program. Three common methods can help locate bottlenecks.
Using the timeit Library
The built‑in timeit module measures the execution time of small code snippets accurately.
import timeit
def Fibonacci():
a, b = 0, 1
i = 0
while i < 100:
print(b)
a, b = b, a + b
i += 1
result = timeit.timeit(Fibonacci, number=5)
print(f"Fibonacci function runtime: {result}")Using memory_profiler
The third‑party memory_profiler library shows memory usage per line, helping locate memory‑intensive code.
from memory_profiler import profile
@profile
def Fibonacci():
a, b = 0, 1
i = 0
while i < 100:
print(b)
a, b = b, a + b
i += 1
Fibonacci()Using line_profiler
line_profiler provides per‑line execution time statistics.
from line_profiler import LineProfiler
def Fibonacci():
a, b = 0, 1
i = 0
while i < 100:
print(b)
a, b = b, a + b
i += 1
lp = LineProfiler()
lp_wrap = lp(Fibonacci)
lp_wrap()
lp.print_stats()Six Practical Optimization Techniques
1. Use Built‑in Functions and Libraries
Built‑in functions are implemented in C and run much faster than equivalent pure‑Python code.
my_list = []
word_list = "hello,world"
for word in word_list:
my_list.append(word.upper())
print(my_list)Better approach using map :
my_list = map(str.upper, word_list)
print(list(my_list))2. Use f‑strings for String Interpolation
f‑strings are more concise and faster than str.format or % formatting.
places = 3
number = 1.23456
my_str = f"number值和places值分别为{number}和{places}"
print(my_str)3. Use List Comprehensions
List comprehensions execute in C and are faster than explicit loops.
my_list = [i for i in range(1, 100) if i % 2 == 1]
print(my_list)4. Cache Results with functools.lru_cache
Caching avoids repeated expensive computations.
import functools, time
@functools.lru_cache(maxsize=2)
def my_func(x):
time.sleep(2)
return x
print(my_func(1))
print("=========")
print(my_func(1)) # Returned instantly from cache5. Optimize Loop Structures
Avoid unnecessary attribute lookups and repeated operations inside loops.
my_list = []
word_list = ["hello,", "word"]
lower = str.lower
for word in word_list:
my_list.append(lower(word))
print(my_list)6. Choose Appropriate Algorithms and Data Structures
Using efficient algorithms (e.g., binary search) and proper data structures dramatically improves performance.
# Binary search example
def binary_search(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
else:
return mid
return -1
nums = [1, 8, 10, 11, 22]
print(binary_search(nums, 11))These six techniques help Python developers write faster, more efficient code.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.