Python Performance Optimization Tips
This article presents practical Python performance optimization techniques—including using local variables, minimizing function calls, avoiding repeated loop calculations, preferring direct imports, leveraging dictionaries for lookups, iterating over items, employing generator expressions, and pre‑compiling code—to improve speed and reduce resource consumption.
Mastering a few techniques can significantly improve Python program performance and avoid unnecessary resource waste.
1. Use local variables – Prefer local over global variables to enhance maintainability, speed, and memory usage. For example, assign ls = os.linesep instead of repeatedly accessing os.linesep.
2. Reduce function call count – When checking an object's type, isinstance() is the most efficient, followed by identity comparison with is, and finally type(). Example:
#判断变量num是否为整数类型
type(num) == type(0) # calls three functions
type(num) is type(0) # identity comparison
isinstance(num, (int)) # calls one function3. Avoid repeated operations in loop conditions – Compute expensive expressions like len(a) once before the loop:
#每次循环都需要重新执行len(a)
while i < len(a):
statement
#len(a)仅执行一次
m = len(a)
while i < m:
statement4. Import specific objects directly – Use from X import Y instead of import X; X.Y to save one lookup.
5. Use mappings instead of conditional chains – Dictionary lookups are far faster than multiple if/elif statements. Example:
#dict查找,性能更优
d = {1:10, 2:20, ...}
b = d[a]6. Iterate over sequence elements directly – Looping over items is quicker than looping over indices:
a = [1,2,3]
for item in a:
print(item)
# versus
for i in range(len(a)):
print(a[i])7. Prefer generator expressions over list comprehensions – Generators produce items lazily, reducing memory usage for large data sets:
#生成器表达式
l = sum([len(word) for line in f for word in line.split()])
#列表解析(会创建完整列表)
l = sum(len(word) for line in f for word in line.split())8. Compile code before execution – Use compile() to obtain a code object and then call eval() or exec(), avoiding repeated compilation. The same applies to regular expressions with re.compile().
9. Adopt module programming habits – Place executable code inside functions and guard test code with if __name__ == '__main__': to prevent unwanted execution on import.
The article concludes with a QR code offering a free Python public course and additional learning resources.
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.
