科迪的天空2正版
43.96MB · 2025-11-21
本篇我们一起搞定两大核心工具:
最后我会给你一个完整的优化示例,让性能提升看得见。
Python 是解释型语言,灵活但速度不是强项。 代码变慢常见原因包括:
搞清楚瓶颈在哪里,是所有优化的起点。
timeit 是个轻量级工具,非常适合比较小片段代码的速度。
例如:列表推导 vs for 循环,谁更快?
import timeit
# 列表推导
expr1 = "[i*i for i in range(1000)]"
# 普通循环
expr2 = """
result = []
for i in range(1000):
result.append(i*i)
"""
t1 = timeit.timeit(expr1, number=10000)
t2 = timeit.timeit(expr2, number=10000)
print("列表推导:", t1)
print("for 循环:", t2)
列表推导: 0.32
for 循环: 0.45
你一下就看出列表推导更快。 这就是 timeit 的价值: 用数字说话,而不是猜测。
当你不确定慢的是哪部分时,就用 cProfile。
它会告诉你:
超级适合分析中大型脚本。
def slow_func():
total = 0
for i in range(10000):
for j in range(10000):
total += i + j
return total
现在我们用 cProfile 分析:
import cProfile
cProfile.run("slow_func()")
你会看到类似:
200000000 function calls in 3.821 seconds
ncalls tottime percall cumtime function
1 3.821 3.821 3.821 slow_func
一下就能看出: 问题全在 slow_func 本身,特别是双层循环。
默认输出较难读,你可以这样格式化:
import cProfile
import pstats
with cProfile.Profile() as pr:
slow_func()
stats = pstats.Stats(pr)
stats.sort_stats(pstats.SortKey.TIME)
stats.print_stats()
如果你想可视化展示(像瀑布图一样),可以安装 SnakeViz:
pip install snakeviz
snakeviz output.prof
非常适合技术分享或团队协作。
假设我们有一个脚本统计每行是否包含某些关键词:
def check_keywords(lines, keywords):
result = []
for line in lines:
for k in keywords:
if k in line:
result.append((line, k))
return result
现在,我们用 cProfile 跑一下:
cProfile.run("check_keywords(lines, keywords)")
你会看到绝大多数时间花在:
str.__contains__
因为你对每个 line、每个 keyword 都在做 in 操作。
import re
def fast_check_keywords(lines, keywords):
pattern = re.compile("|".join(map(re.escape, keywords)))
result = []
for line in lines:
m = pattern.search(line)
if m:
result.append((line, m.group()))
return result
从 2.5 秒 → 0.01 秒 优化效果巨大。
不要凭直觉优化 永远先用 timeit / cProfile 查看瓶颈。
例如:
缓存、预编译、提前转换格式,都能救命。
性能优化是 Python 开发者进阶的必备技能。
timeit 解决“小范围速度比较”,
cProfile 负责“全局瓶颈定位”。
只要你掌握:
你就可以让脚本从“能跑”变成“飞快”。