Six Techniques to Improve Python Code Performance
This article introduces six practical techniques—including profiling with timeit, memory and line profilers, using built‑in functions, f‑strings, list comprehensions, and lru_cache—as well as algorithm and data‑structure choices to help Python developers significantly boost the execution speed of their code.
Many developers complain that Python runs slowly, but execution speed largely depends on the programmer's algorithmic skills and code optimization. While Python may lag in raw performance, it excels in development speed, and the following techniques can help close the gap.
Code Performance Detection – Before optimizing, identify bottlenecks. The article demonstrates three methods using a simple Fibonacci implementation.
# 斐波那契数列
def Fibonacci():
a, b = 0, 1
i = 0
while i < 100:
print(b)
a, b = b, a+b
i += 1
Fibonacci()Using the timeit library – timeit measures execution time of small code snippets.
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函数的运行时间为: {result}")Using memory_profiler – This third‑party library shows per‑line memory usage.
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 – Similar to memory_profiler, it measures execution time per line.
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()Using built‑in functions and libraries – Built‑ins are implemented in C and run much faster than pure Python equivalents.
my_list = []
word_list = "hello,world"
for word in word_list:
my_list.append(word.upper())
print(my_list)Improved version with map:
word_list = "hello,world"
my_list = map(str.upper, word_list)
print(list(my_list))Using f‑strings – Formatted string literals 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)Using list comprehensions – They execute in C and are faster than equivalent loops.
my_list = [i for i in range(1, 100) if i % 2 == 1]
print(my_list)Using functools.lru_cache decorator – Caches function results to avoid repeated computation.
import time
import functools
@functools.lru_cache(maxsize=2)
def my_func(x):
time.sleep(2)
return x
print(my_func(1))
print("=========")
print(my_func(1)) # Cached result returned instantlyOptimizing loop structures – Reduce unnecessary attribute lookups and repeated operations.
my_list = []
word_list = ["hello,", "word"]
lower = str.lower
for word in word_list:
my_list.append(lower(word))
print(my_list)Choosing appropriate algorithms and data structures – Selecting efficient algorithms (e.g., binary search) and proper data structures can dramatically improve performance.
# Binary search example
def binary_search(nums, target):
first, last = 0, len(nums) - 1
while first <= last:
mid = (first + last) // 2
if nums[mid] < target:
first = mid + 1
elif nums[mid] > target:
last = mid - 1
else:
return mid
return -1
nums = [1, 8, 10, 11, 22]
print(binary_search(nums, 11))In summary, applying these six Python performance‑enhancing techniques—profiling, leveraging built‑ins, modern syntax, caching, loop optimization, and algorithmic choices—can help developers write faster, more efficient code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
