0%

Thread Free Python 性能测试

引言

Python 3.13 版本引入了 (No-GIL 模式),彻底解决了长期存在的并发性能瓶颈问题。随着Python 3.14的发布,Non-GIL 模式更加稳定。本文通过一个计算密集型任务的实际测试,对比 GIL 模式和 No-GIL 模式的性能差异,展示 Python 3.14 在多线程场景下的显著提升。

测试环境

  • 操作系统: Ubuntu 24.04 (WSL 环境)
  • Python 版本:
    • GIL 模式: Python 3.14
    • No-GIL 模式: Python 3.14t (Thread-Free)
  • 执行工具: uv (高性能 Python 包安装器和解析器)
  • 硬件配置: ThinkBook 14 , i5-13500H (2.60 GHz), 32GB RAM

测试方案设计

我们设计了一个计算密集型任务:计算 1 到 1,000,000 的平方和。通过以下方式验证并行性能:

  1. 任务分解:

    • 将数据集划分为与 CPU 核心数相等的块
    • 每个线程处理一个数据块
  2. 计算函数:

1
2
def sum_of_squares(numbers: list[int]):
return reduce(lambda x, y: x + y**2, numbers)
  1. 并发框架:
    • 使用 ThreadPoolExecutor 实现线程池
    • 每次测试运行 10 次取平均值

测试代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from concurrent.futures import ThreadPoolExecutor
from functools import reduce
from time import perf_counter
import os

def sum_of_squares(numbers: list[int]):
"""计算数字列表的平方和"""
return reduce(lambda x, y: x + y**2, numbers)

if __name__ == "__main__":
MAX_WORKERS = os.cpu_count()
print(f"Using {MAX_WORKERS} threads")

# 创建测试数据集 (1-1,000,000)
numbers = list(range(1, 1000001))

# 数据分块
chunk_size = len(numbers) // MAX_WORKERS
chunks = [numbers[i : i + chunk_size] for i in range(0, len(numbers), chunk_size)]

total_time = 0
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
for _ in range(10): # 运行10次取平均
start = perf_counter()
results = list(executor.map(sum_of_squares, chunks))
end = perf_counter()
total_time += end - start

average_time = total_time / 10
print(f"Average time over 10 runs: {average_time:.4f} seconds")

测试结果分析

使用不同 Python 版本运行上述测试:

1
2
3
4
5
6
7
8
9
# GIL 模式
uv run -p 3.14 python test-gil.py
Using 16 threads
Average time over 10 runs: 0.0420 seconds

# No-GIL 模式
uv run -p 3.14t python test-gil.py
Using 16 threads
Average time over 10 runs: 0.0149 seconds

性能对比

Python 版本 执行时间 (秒) 加速比
Python 3.14 (GIL) 0.0420 1.0x
Python 3.14t (No-GIL) 0.0149 2.82x

No-GIL 模式展现出 2.8 倍 的性能提升,充分释放了多核 CPU 的计算潜力。

技术解析

  1. GIL 限制:

    • 传统 Python 中,GIL 阻止多线程真正并行执行
    • CPU 密集型任务受限于单线程执行
  2. No-GIL 架构:

    • 移除全局解释器锁
    • 使用原子操作和细粒度锁管理内存
    • 线程可真正并行执行字节码
  3. 性能关键点:

    • 计算任务完全 CPU 密集
    • 无 I/O 阻塞操作
    • 数据分块策略有效减少线程竞争

结论与启示

Python 3.14 的 No-GIL 模式在计算密集型多线程场景中展现出显著优势:

  • 显著的性能提升,充分利用多核硬件
  • 无缝兼容现有 Python 代码库
  • 简化并发模型,无需转向 multiprocessing

这一改进使 Python 在科学计算、数据处理等领域的竞争力大幅提升。随着 No-GIL 的持续优化,Python 正从”胶水语言”向高性能计算平台演进。

Python 3.14 的发布标志着解释器架构的重大进步,为 Python 的高性能计算铺平了道路。

扫码加入技术交流群🖱️
QR code