Python Code Optimization Techniques and Performance Tips
This article presents Python code optimization principles, including avoiding premature optimization, reducing global variable usage, minimizing attribute access, eliminating unnecessary abstractions, optimizing loops, leveraging numba JIT, and selecting appropriate data structures, accompanied by performance comparisons and code examples.
Python, while slower than compiled languages, can be significantly accelerated by applying various optimization techniques.
Optimization Principles – Do not optimize prematurely, weigh the cost of optimization, and focus on performance‑critical parts of the code.
Avoid Global Variables – Moving code into functions can improve speed by 15‑30%.
# Not recommended
import math
size = 10000
for x in range(size):
for y in range(size):
z = math.sqrt(x) + math.sqrt(y)
# Recommended
def main():
size = 10000
for x in range(size):
for y in range(size):
z = math.sqrt(x) + math.sqrt(y)
main()Reduce Module and Attribute Access – Import specific functions and bind frequently used methods to local variables.
# First optimization
from math import sqrt
def computeSqrt(size):
result = []
for i in range(size):
result.append(sqrt(i))
return resultAvoid Class Attribute Access – Cache instance attributes in local variables inside methods.
# Recommended
class DemoClass:
def __init__(self, value):
self._value = value
def computeSqrt(self, size):
result = []
append = result.append
sqrt = math.sqrt
val = self._value
for _ in range(size):
append(sqrt(val))
return resultEliminate Unnecessary Abstractions – Remove property getters/setters and other wrappers when they add no functional benefit.
# Recommended
class DemoClass:
def __init__(self, value):
self.value = value
def compute(self, size):
result = []
for i in range(size):
result.append(self.value)
return resultData Copy Avoidance – Do not create intermediate lists when a single comprehension suffices.
# Recommended
def main():
size = 10000
value = range(size)
square_list = [x*x for x in value]Loop Optimizations – Prefer for over while , use implicit loops, and move invariant calculations outside inner loops.
# Move sqrt out of inner loop
for x in range(size):
sqrt_x = sqrt(x)
for y in range(size):
z = sqrt_x + sqrt(y)Numba JIT Compilation – Decorating functions with @numba.jit can reduce execution time from seconds to fractions of a second.
@numba.jit
def computeSum(size):
total = 0
for i in range(size):
total += i
return totalChoosing the Right Data Structure – Use built‑in containers such as list , deque , bisect , or heapq to achieve optimal time complexity for insertion, deletion, and lookup operations.
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.