Hacker News 中文摘要

RSS订阅

Python 3.14 来了,它有多快? -- Python 3.14 is here. How fast is it?

文章摘要

文章作者在2025年测试了新发布的Python 3.14性能,通过纯Python代码基准测试对比历史版本,发现Python性能持续提升,但提醒读者这类通用基准测试存在局限性,不能完全反映解释器的真实性能表现。

文章总结

Python 3.14性能实测:速度提升显著,多线程表现亮眼

作者Miguel Grinberg于2025年10月8日发布了针对Python 3.14的性能基准测试报告。这是继2024年11月《Python真的很慢吗?》之后的最新性能对比研究。

测试方法说明: 1. 测试环境: - 硬件:搭载Intel i5的Ubuntu笔记本和M2芯片的MacBook - 对比版本:CPython 3.9-3.14、Pypy 3.11、Node.js 24和Rust 1.90 - 新增特性测试:3.13+版本的JIT编译器和无GIL的自由线程(FT)解释器

  1. 测试用例:
  • fibo.py:递归计算第40个斐波那契数
  • bubble.py:对1万个随机数进行冒泡排序

关键发现:

一、单线程性能 1. 斐波那契测试: - 3.14比3.13快27%(Linux 6.59秒 vs 8.26秒) - Pypy表现惊人,比3.14快4.93倍 - Rust以69倍优势遥遥领先

  1. 冒泡排序测试:
  • 3.14比3.13快22%(Linux 2.18秒 vs 2.82秒)
  • Pypy在此测试中优势更大,达到18倍领先

二、新解释器特性 1. JIT编译器: - 当前版本提升有限(冒泡排序测试中最高仅16%) - 作者推测可能对递归函数优化不足

  1. 自由线程解释器:
  • 单线程模式仍有性能损耗(3.14 FT为标准版的91%)
  • 多线程测试中展现巨大潜力:
    • 斐波那契测试提速3.09倍
    • 冒泡排序测试提速2.03倍

三、历史版本对比 - 3.11是重要转折点,性能从"非常慢"提升至"尚可接受" - 3.14成为迄今最快的CPython版本

实用建议: 1. 优先升级到3.14或至少3.11+版本 2. CPU密集型多线程应用建议尝试自由线程解释器 3. 对性能有极致要求可考虑Pypy(但需注意兼容性) 4. 当前JIT编译器效果有限,建议观望后续版本

测试局限性: 1. 纯Python代码测试,不包含C扩展模块 2. 测试用例较简单(递归和迭代场景) 3. 完整测试脚本已开源在GitHub仓库

(完整测试数据和图表请参阅原文,测试代码仓库:https://github.com/miguelgrinberg/pyspeed)

评论总结

评论内容总结:

1. Python性能的局限性

  • 主要观点:Python的性能提升有限,无法与JIT或静态编译语言相比。
  • 论据
    • "fast python" is an oxymoron... the language hasn't fundamentally changed. (ModernMech)
    • Python is not the language for you if perf is a top priority. (ModernMech)
    • Two orders of magnitude slower than Rust. (username223)

2. Python的适用场景

  • 主要观点:Python的优势在于快速原型设计和系统集成,而非高性能计算。
  • 论据
    • Python isn't built for raw performance and that's totally fine! (cat-whisperer)
    • It's a dynamic, interpreted language good for prototyping and gluing systems together. (ModernMech)

3. 对GIL和JIT的讨论

  • 主要观点:GIL移除和JIT的改进效果有限,PyPy表现优于CPython。
  • 论据
    • PyPy is faster than free threaded CPython even on multi threaded code. (veber-alex)
    • The initial goal for JIT is stability, not performance. (alfalfasprout)

4. 对Python未来的期望

  • 主要观点:希望Python能持续改进性能,同时保持其易用性。
  • 论据
    • I wonder if GIL-less Python will one day enable GIL-less C FFI? (didip)
    • Python is doing great stuff to squeeze out performance from a scripting language. (librasteve)

5. 幽默和调侃

  • 主要观点:对Python版本号(3.14)和性能的调侃。
  • 论据
    • Py = 3.14 :') (Curzel)
    • more like "pi"thon (mwkaufma)

6. 其他语言的对比

  • 主要观点:其他语言(如Rust、NodeJS、Raku)在性能上表现更优。
  • 论据
    • Rust is indeed quite fast. (moralestapia)
    • Raku has fewer native libraries but could compare one day. (librasteve)

7. 对PyPy的讨论

  • 主要观点:PyPy未被广泛使用的原因和潜力。
  • 论据
    • Why nobody uses pypy? (modeless)
    • PyPy is still a separate distinct track from mainline Python. (didip)

8. 对性能改进的肯定

  • 主要观点:尽管性能有限,但改进仍然值得肯定。
  • 论据
    • That >2x performance increase over 3.9 is pretty impressive. (lenerdenator)
    • The performance will come for languages we love. (dzonga)