Python Code Optimization Techniques to Accelerate Execution
This article presents a collection of practical Python performance‑enhancing techniques, covering optimization principles, avoidance of global variables and unnecessary abstractions, loop refinements, use of local bindings, short‑circuit logic, JIT compilation with numba, and selecting appropriate data structures to achieve significant speed‑ups.
0. Code Optimization Principles
Before diving into specific tricks, understand three basic principles: avoid premature optimization, weigh the cost of optimization, and focus on the parts of code that actually affect performance.
1. Avoid Global Variables
# Not recommended. Time: 26.8 s
import math
size = 10000
for x in range(size):
for y in range(size):
z = math.sqrt(x) + math.sqrt(y)Placing code in a function reduces lookup time for variables, typically yielding a 15‑30% speed improvement.
# Recommended. Time: 20.6 s
import math
def main():
size = 10000
for x in range(size):
for y in range(size):
z = math.sqrt(x) + math.sqrt(y)
main()2. Avoid Module and Function Attribute Access
# First optimization. Time: 10.9 s
from math import sqrt
def computeSqrt(size: int):
result = []
for i in range(size):
result.append(sqrt(i))
return resultBinding frequently used functions (e.g., sqrt) to local variables eliminates repeated attribute lookups.
# Recommended. Time: 7.9 s
import math
def computeSqrt(size: int):
result = []
append = result.append
sqrt = math.sqrt
for i in range(size):
append(sqrt(i))
return result3. Avoid Class Attribute Access
# Not recommended. Time: 10.4 s
class DemoClass:
def __init__(self, value: int):
self._value = value
def computeSqrt(self, size: int):
result = []
for _ in range(size):
result.append(math.sqrt(self._value))
return resultCache class attributes in local variables to speed up inner‑loop operations.
# Recommended. Time: 8.0 s
class DemoClass:
def __init__(self, value: int):
self._value = value
def computeSqrt(self, size: int):
result = []
append = result.append
sqrt = math.sqrt
value = self._value
for _ in range(size):
append(sqrt(value))
return result4. Avoid Unnecessary Abstraction
# Not recommended. Time: 0.55 s
class DemoClass:
def __init__(self, value: int):
self.value = value
@property
def value(self) -> int:
return self._value
@value.setter
def value(self, x: int):
self._value = xRemoving property getters/setters and using plain attributes cuts overhead.
# Recommended. Time: 0.33 s
class DemoClass:
def __init__(self, value: int):
self.value = value5. Avoid Data Copying
# Not recommended. Time: 6.5 s
def main():
size = 10000
for _ in range(size):
value = range(size)
value_list = [x for x in value]
square_list = [x * x for x in value_list]Eliminate intermediate containers when they are not needed.
# Recommended. Time: 4.8 s
def main():
size = 10000
for _ in range(size):
value = range(size)
square_list = [x * x for x in value]6. Use Short‑Circuit Logic in Conditions
# Recommended. Time: 0.03 s
def concatString(string_list: List[str]) -> str:
abbreviations = {'cf.', 'e.g.', 'ex.', 'etc.', 'flg.', 'i.e.', 'Mr.', 'vs.'}
result = ''
for str_i in string_list:
if str_i[-1] == '.' and str_i in abbreviations:
result += str_i
return result7. Loop Optimizations
Replace while loops with for loops, use implicit loops (e.g., sum(range(size))), and move invariant calculations out of inner loops.
# Recommended. Time: 1.7 s
def computeSum(size: int) -> int:
return sum(range(size))8. Use numba.jit
# Recommended. Time: 0.62 s
import numba
@numba.jit
def computeSum(size: float) -> int:
sum = 0
for i in range(size):
sum += i
return sum9. Choose Appropriate Data Structures
Built‑in containers ( list, dict, set, etc.) are implemented in C and fast; for frequent insert/delete use collections.deque, and for fast look‑ups consider bisect or heapq to maintain ordered collections.
Original source: Zhihu article
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.
