cpython freethread 性能在几个版本中的改进

张开发
2026/4/19 8:34:54 15 分钟阅读

分享文章

cpython freethread 性能在几个版本中的改进
cpython 3.13版本开始引入freethread 功能提高了多线程的执行效率。参考文档 https://docs.pythonlang.cn/3/howto/free-threading-python.html但是官方发布的版本都是没有用freethread选项编译的。uv提供了freethread选项编译的版本。测试脚本# stress_test.pyimportthreadingimporttimeimportmathdefcpu_bound_task(n):total0foriinrange(n):totalmath.sqrt(i)returntotaldefrun_sequential(n,workers):starttime.time()for_inrange(workers):cpu_bound_task(n)returntime.time()-startdefrun_threaded(n,workers):threads[]starttime.time()for_inrange(workers):tthreading.Thread(targetcpu_bound_task,args(n,))threads.append(t)t.start()fortinthreads:t.join()returntime.time()-startif__name____main__:N5_000_000WORKERS4seq_timerun_sequential(N,WORKERS)thread_timerun_threaded(N,WORKERS)print(fSequential time:{seq_time:.2f}s)print(fThreaded time:{thread_time:.2f}s)print(fSpeedup:{seq_time/thread_time:.2f}x)可以从南京大学镜像https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone/20260414/下载uv的普通版本和freethread版本python绿色解压版。3.13#普通版本 C:\d\cpython313python ..\st_test.py Sequential time: 0.87s Threaded time: 0.93s Speedup: 0.94x #freethread版本 C:\d\cpython313tpython ..\st_test.py Sequential time: 1.48s Threaded time: 0.38s Speedup: 3.85x3.14#普通版本 C:\dcurl -LO https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone/20260414/cpython-3.14.4%2B20260414-x86_64-pc-windows-msvc-install_only_stripped.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 21.31M 100 21.31M 0 0 26.81M 0 0 C:\d\cpython3144python ..\st_test.py Sequential time: 1.10s Threaded time: 1.19s Speedup: 0.92x #freethread版本 C:\dcurl -LO https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone/20260414/cpython-3.14.4%2B20260414-x86_64-pc-windows-msvc-freethreaded-install_only_stripped.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 21.52M 100 21.52M 0 0 2.25M 0 00:09 00:09 2.81M C:\d\cpython3144tpython ..\st_test.py Sequential time: 1.47s Threaded time: 0.30s Speedup: 4.83x3.15#普通版本 C:\dcurl -LO https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone/20260414/cpython-3.15.0a8%2B20260414-x86_64-pc-windows-msvc-install_only_stripped.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 22.17M 100 22.17M 0 0 25.63M 0 0 C:\d\cpython315a8python ..\st_test.py Sequential time: 0.87s Threaded time: 0.94s Speedup: 0.92x #freethread版本 C:\dcurl -LO https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone/20260414/cpython-3.15.0a8%2B20260414-x86_64-pc-windows-msvc-freethreaded-install_only_stripped.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 22.39M 100 22.39M 0 0 2.67M 0 00:08 00:08 3.23M C:\d\cpython315a8tpython ..\st_test.py Sequential time: 0.96s Threaded time: 0.26s Speedup: 3.67x可见这个脚本多线程速度这3.13、3.14、3.15a几个版本freethread稳步提高顺序执行还是普通版本更快。3.14的普通版最慢。

更多文章