3

证明PyPy比Python更快的5个性能基准 - codex

 2 years ago
source link: https://www.jdon.com/57545
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
证明PyPy比Python更快的5个性能基准 - codex

对于许多用 Python 编写的程序来说,糟糕的性能并不是什么大问题。

但是,如果我们想让我们的 Python 更快呢?切换到另一种编程语言?

你可以使用PyPy Just-in-Time 编译器来运行,根据官网的说法,即使是 Python 创建者,Guido von Rossum 也建议将 PyPy 用于性能关键的 Python 程序。

基准测试的工作原理

为了比较 Python 和 PyPy,我编写了几个 Python 程序。著名算法、典型用例,甚至是基本的 HTTP 服务器。

使用的版本:

  • PyPy: 7.3.5,使用 Python 版本 3.7.10
  • Python:版本 3.9.7

测试案例 

  • 斐波那契测试

Python 平均需要 2337 毫秒的执行时间。

PyPy 平均只需要 301 毫秒。

明显的赢家是 PyPy。

  • 网络服务器

Python: Web 服务器平均每秒可以处理 995 个请求,平均延迟为 2.03 毫秒。

PyPy: Web 服务器平均每秒可以处理 1481 个请求,平均延迟为 1.90 毫秒。

Python:平均而言,代码执行需要 43 毫秒

PyPy:平均执行时间为 132 毫秒。

是的,Python 在这里更快。

Python:代码平均耗时 2.89 秒

PyPy:平均需要 69 毫秒。

是的,我说的是毫秒。在这个基准测试中,PyPy 比普通 Python 快几个数量级。

  • SQLite3 数据库

Python 平均需要 6.7 秒来执行代码。PyPy 平均需要 9.4 秒的执行时间。

Python 速度更快。

我还尝试将其与其他操作结合使用——比如删除刚刚创建的条目。它没有改变结果。在 SQlite3 数据库的情况下,普通Python 比 PyPy 快。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK