简体   繁体   English

为什么 Python 比 Ruby 快?

[英]Why is Python faster than Ruby?

They seem to share a lot of the same characteristics but as far as I can tell, Python 2.5 is faster than 1.8.7 by a lot.它们似乎有很多相同的特征,但据我所知,Python 2.5 比 1.8.7 快很多。

Is there a deeper underlying reason behind this?这背后是否有更深层次的原因?

Nothing deep , I am pretty sure -- it's strictly a matter of implementation choices and maturity.没什么深奥的,我很确定——这完全是实现选择和成熟度的问题。 Python was quite a bit slower in many aspects not so long ago, after all!毕竟不久前,Python 在许多方面都慢了很多! Consider for example:考虑例如:

$ py24 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 10.8 usec per loop
$ py25 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 9.83 usec per loop
$ py26 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 8.12 usec per loop
$ py27 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 6.35 usec per loop

Yep, all on the same machine (Macbook Pro, 2.4 GHz Intel Core 2 Duo, OSX 10.5), all "official" Mac releases from python.org (latest one of each x in the 2.x series).是的,都在同一台机器上(Macbook Pro,2.4 GHz Intel Core 2 Duo,OSX 10.5),所有来自 python.org 的“官方”Mac 版本( 2.x系列中每个x最新版本)。 I have no 2.3 around to check, but I'd expect it to be a wee bit slower than 2.4.我没有 2.3 可供检查,但我希望它比 2.4 慢一点。

This is just the kinds of speed-up that a lot of loving, painstaking work can achieve among successive releases of pretty much the same underlying architecture.这正是在几乎相同的底层架构的连续版本中,许多充满爱心、艰苦的工作可以实现的加速。 Not as flashy as adding feechurz, but often vastly more useful in the real world!-)不像添加feechurz那么华而不实,但在现实世界中通常更有用!-)

I'm pretty sure, therefore, that Ruby can also stabilize on a sound, performance-robust underlying architecture, then start getting a steady stream of under-the-hood performance tweaks over the years to get (eg) the 40% or so further improvement we observe here has been happening in (at least some parts of) Python in the last few years.因此,我很确定 Ruby 也可以稳定在一个健全的、性能稳健的底层架构上,然后多年来开始进行稳定的底层性能调整以获得(例如)40% 左右在过去的几年中,我们在此处观察到的(至少是某些部分的)Python 发生了进一步的改进。

One reason is Python's being compiled into bytecode which is then executed by a highly optimized VM.一个原因是 Python 被编译成字节码,然后由高度优化的 VM 执行。 AFAIK Ruby doesn't work this way in 1.8 and earlier - but interprets the trees on the fly. AFAIK Ruby 在 1.8 及更早版本中不能以这种方式工作 - 但会动态解释树。

Think of it this way:可以这样想:

Python: Python:

  1. Parse code into ASTs将代码解析为 AST
  2. Convert ASTs into bytecode将 AST 转换为字节码
  3. Run bytecode on a VM在 VM 上运行字节码

Ruby (prior to 1.9): Ruby(1.9 之前):

  1. Parse code into ASTs将代码解析为 AST
  2. Interpret the ASTs directly by recursive traversal通过递归遍历直接解释 AST

Without getting too much into detail, step 2 in the old Ruby has a lot of repetitions because it has to "understand" the ASTs each time it sees them (which, in an inner loop is a lot ).无需过多赘述,旧 Ruby 中的第 2 步有很多重复,因为它每次看到 AST 时都必须“理解”它们(这在内部循环中很多)。 Python "understands" the ASTs only once, and then the VM runs the bytecode as fast as it can (which isn't different in principle from the way the Java and .NET VMs work). Python 只“理解”AST 一次,然后 VM 会尽可能快地运行字节码(这与 Java 和 .NET VM 的工作方式原则上没有区别)。

Ruby 1.9 moved to YARV , which is also a VM-based approach. Ruby 1.9 移至YARV ,这也是一种基于 VM 的方法。 Ruby 1.9 is faster than 1.8 . Ruby 1.9 比 1.8 快 Here's a quote from the creator of YARV , Koichi Sasada:是 YARV 的创建者Koichi Sasada 的 一句话

At first, YARV is simple stack machine which run pseudo sequential instructions.起初,YARV 是运行伪顺序指令的简单堆栈机器。 Old interpreter (matzruby) traverses abstract syntax tree (AST) naively.旧解释器 (matzruby) 天真地遍历抽象语法树 (AST)。 Obviously it's slow.显然它很慢。 YARV compile that AST to YARV bytecode and run it. YARV 将该 AST 编译为 YARV 字节码并运行它。

An interesting point to note is that the Python VM is also stack based, just like YARV.需要注意的一个有趣点是 Python VM 也是基于堆栈的,就像 YARV 一样。

Because Ruby 1.8 was not really designed with performance in mind, while Python was more optimized.因为 Ruby 1.8 的设计并没有真正考虑性能,而 Python 则更加优化。 In particular, Ruby 1.8 did real interpretation rather than compiling for a virtual machine like most languages these days.特别是,Ruby 1.8 进行了真正的解释,而不是像当今大多数语言那样为虚拟机进行编译。 Ruby 1.9 (with the YARV VM) is about as fast as Python 3 (maybe a little bit slower, but much closer), and other implementations are even faster. Ruby 1.9(带有 YARV VM)大约与 Python 3 一样快(可能慢一点,但更接近),其他实现甚至更快。

I read the answers and I see most people are saying "oh you should not compare to Ruby 1.8, you should go with 1.9, it's much faster".我阅读了答案,我看到大多数人都在说“哦,您不应该与 Ruby 1.8 进行比较,您应该使用 1.9,它要快得多”。 Well ok, why not just look at some benchmarks?好吧,为什么不看看一些基准呢?

So here is how Ruby 1.9 (YARV) fares against Ruby 1.8 (MRI): http://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=yarv&lang2=ruby所以这里是 Ruby 1.9 (YARV) 与 Ruby 1.8 (MRI) 的对比: http ://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=yarv&lang2=ruby

And here is how Ruby 1.9 compares to Python 2.x: http://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=yarv&lang2=python以下是 Ruby 1.9 与 Python 2.x 的比较: http ://shootout.alioth.debian.org/u32/benchmark.php?test = all&lang = yarv&lang2 = python

To recap, Ruby 1.9 is about 2x faster than Ruby 1.8 - but still is slower - 2x slower - than Python.总结一下,Ruby 1.9 比 Ruby 1.8大约 2- 但仍然比 Python- 慢 2


PS.附注。 I guess I need to clarify after Chuck's objections: I don't see the Computer Language Shootout as the definitive answer to the questions of Life, the Universe and Everything .我想我需要在 Chuck 的反对意见之后澄清:我不认为计算机语言大战生命、宇宙和一切问题的最终答案。 Far from it.离得很远。 I would be glad to be referred to other objective sources.我很高兴被提及其他客观来源。

I would also be glad to hear informal/subjective results from people here on S/O, provided they have participated in over 50 discussions on Python or Ruby and their Ruby/Python bias is within +/-5dB (Ruby/Python Ratio calculated as RPR=10*log10(numTags('Ruby')/numTags('Python')) dB; thus for user Chuck that would be 10*log10(225/13) = 12dB, mine is -10 - we both cannot be relied on unbiased opinion) :-)我也很高兴听到 S/O 上人们的非正式/主观结果,前提是他们参与了 50 多次关于 Python 或 Ruby 的讨论,并且他们的 Ruby/Python 偏差在 +/-5dB(Ruby/Python 比率计算为RPR=10*log10(numTags('Ruby')/numTags('Python')) dB;因此对于用户 Chuck 来说,这将是 10*log10(225/13) = 12dB,我的是 -10 - 我们都不能信赖在公正的意见) :-)

More people have been working on Python development for more years, so more optimization has been done.更多的人从事Python开发工作多年,所以做了更多的优化。 The languages are similarly flexible and expressive, so their performance should converge as all the good optimization ideas get used in both.这些语言同样灵活且富有表现力,因此当所有好的优化思想都用于两者时,它们的性能应该会趋于一致。 As noted above, Ruby 1.9 substantially narrows the performance gap with Python.如上所述,Ruby 1.9 大大缩小了与 Python 的性能差距。

It depends on the implementation.这取决于实现。 Crystal is basically C compiled Ruby that can even call C libraries. Crystal 基本上是 C 编译的 Ruby,甚至可以调用 C 库。 Then you also have Elixir on the Beam side and let's not forget the Java and C# counterparts.然后你在 Beam 端也有 Elixir,不要忘记 Java 和 C# 对应物。 But yes, Ruby, the defacto standard, is indeed slower than Python and is also aimed at web development.但是是的,事实上的标准 Ruby 确实比 Python 慢,而且还针对 Web 开发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM