哪个更快:MRI Ruby 还是 JRuby?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4895684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is faster: MRI Ruby or JRuby?
提问by ses
If I am using Ruby on Rails, should I install MRI/YARV Ruby or JRuby? Which is faster?
如果我使用 Ruby on Rails,我应该安装 MRI/YARV Ruby 还是 JRuby?哪个更快?
采纳答案by Mikel
The answer depends on many variables.
答案取决于许多变量。
But in general, Ruby 1.9 is faster than JRuby, but Ruby 1.8 is slower than JRuby.
但总的来说,Ruby 1.9 比 JRuby 快,但 Ruby 1.8 比 JRuby 慢。
e.g. according to the Computer Language Benchmarks Game:
例如根据计算机语言基准游戏:
Also, if your application is multi-threaded, JRuby may have some advantages over standard Ruby
(a.k.a. MRI)], depending on how many cores you have.
此外,如果您的应用程序是多线程的,JRuby 可能比标准 Ruby
(又名 MRI)有一些优势],这取决于您拥有的内核数量。
回答by jc00ke
Recent benchmarks put JRuby in the lead, followed by MagLev, Rubinius, then MRI.
最近的基准测试让 JRuby 处于领先地位,其次是 MagLev、Rubinius,然后是 MRI。
Benchmarking is tricky. The Ruby Benchmark Suiteis what most use to benchmark Ruby. If you remove any benchmarks that fail across any implementation, you get the following graph.
基准测试很棘手。该红宝石基准套件是大多数使用的基准红宝石。如果您删除在任何实现中失败的任何基准测试,您将获得下图。


Notice I used the geometric mean. This is a better indicator of average performance as it normalizes outliers and machine specs.
请注意,我使用了几何平均值。这是一个更好的平均性能指标,因为它标准化了异常值和机器规格。
The best benchmark you can run will be specific to your app. If you are looking for overall performance, you'll likely want to use real threads, so Rubinius or JRuby are your only real choices.
您可以运行的最佳基准测试将特定于您的应用程序。如果您正在寻找整体性能,您可能希望使用真正的线程,因此 Rubinius 或 JRuby 是您唯一真正的选择。
Also, each implementation is fast at different things. MRI is veryfast at starting up, so it's good for scripts. For longer-running applications (like a web application) Rubinius or JRuby will generally perform better than MRI.
此外,每个实现在不同的事情上都很快。MRI启动速度非常快,因此非常适合脚本。对于运行时间较长的应用程序(如 Web 应用程序),Rubinius 或 JRuby 通常会比 MRI 表现得更好。
回答by PatrickTulskie
Honestly, it depends on your code. Install RVM or Pik on your machine, install a bunch of different versions of ruby, and try running your code in them.
老实说,这取决于您的代码。在您的机器上安装 RVM 或 Pik,安装一堆不同版本的 ruby,然后尝试在其中运行您的代码。
For example: An application that frequently restarts is not a great candidate for JRuby since JRuby has some ramp-up time before Hotspot is able to effectively optimize your code. Likewise, an application that relies on threads is not a great candidate for Ruby 1.8.7 since Ruby 1.8.X cannot utilize more than 1 core on your processor and thus cannot execute on more than one thread at a time.
例如: 频繁重启的应用程序不是 JRuby 的理想选择,因为 JRuby 在 Hotspot 能够有效优化您的代码之前有一些加速时间。同样,依赖线程的应用程序也不是 Ruby 1.8.7 的理想选择,因为 Ruby 1.8.X 不能在处理器上使用 1 个以上的内核,因此不能一次在多个线程上执行。
回答by 2potatocakes
There's a really great article done by the guys over at programmingzen.com that compares a lot of the different flavours of ruby. Was published in July last year so still reasonably recent ;) There page compares these:
这些人在programmingzen.com 上发表了一篇非常棒的文章,比较了许多不同风格的ruby。于去年 7 月发布,所以仍然是最近的 ;) 页面比较了这些:
* Ruby 1.8.7 p299
* Ruby 1.9.1 p378
* Ruby 1.9.2 RC2
* IronRuby 1.0 (Mono 2.4.4)
* JRuby 1.5.1 (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20)
* MagLev (rev 23832)
* Ruby Enterprise Edition 2010.02
* Rubinius 1.0.1
Might be able to find what you're looking for there
或许可以在那里找到您要找的东西
http://programmingzen.com/2010/07/19/the-great-ruby-shootout-july-2010/
http://programmingzen.com/2010/07/19/the-great-ruby-shootout-july-2010/
回答by user3719235
Actually the answer above is not correct except mikel's answer ,if you observed any benchmark for JVM you'll find it's slow so imagine JRuby's performance compared to CRuby.
实际上,除了 mikel 的答案之外,上面的答案是不正确的,如果您观察了 JVM 的任何基准测试,您会发现它很慢,因此想象一下 JRuby 与 CRuby 相比的性能。
Personally I'm an MRI Ruby contributor and I think that the benchmark chart above is not true at all since the introduction of the YARV VM of MRI Ruby , this version became the fastest outthere and lots of benchmarks proved so "See Pat Shaughnessy's benchmarking and comments about the three famous Ruby interpreters MRI Ruby , JRuby and Rubinius". So in my opinion there is no point of comparison due to the following logical points:-
就我个人而言,我是 MRI Ruby 的贡献者,我认为自从引入 MRI Ruby 的 YARV VM 以来,上面的基准测试图表根本不正确,这个版本成为了最快的版本,并且许多基准测试证明如此“参见 Pat Shaughnessy 的基准测试和关于三个著名的 Ruby 解释器 MRI Ruby、JRuby 和 Rubinius 的评论”。因此,在我看来,由于以下逻辑点,没有比较的意义:-
1- C is much faster than Java because it operates directly on Hardware and produces machine code. While JVM produces Bytecode which is considered intermediate code.
1- C 比 Java 快得多,因为它直接在硬件上运行并生成机器代码。JVM 产生被认为是中间代码的字节码。
2- JRuby contains an additional interpretation step unlike MRI Ruby " Tokenization , Parsing , AST Parsing and generating YARV instructions "Code generation" and finally Code execution" While JRuby contains an additional stage.
2- JRuby 包含一个与 MRI Ruby 不同的额外解释步骤“标记化、解析、AST 解析和生成 YARV 指令”代码生成“最后代码执行”而 JRuby 包含一个额外的阶段。
3- Garbage collection in MRI Ruby is a lot faster than garbage collection in JRuby even it got better when they introduced some changes in the mark and sweep garbage collection technique.
3- MRI Ruby 中的垃圾收集比 JRuby 中的垃圾收集快得多,即使在标记和清除垃圾收集技术中引入了一些更改后,它也变得更好了。
4- If you surfed most of the companies and technologies used they always used MRI Ruby particularly ruby 1.9 , I rarely saw a company using JRuby or even saw lots of extensions or contributions to it unlike MRI Ruby.
4- 如果你浏览过大多数使用的公司和技术,他们总是使用 MRI Ruby,尤其是 ruby 1.9 ,我很少看到一家公司使用 JRuby,甚至看到了与 MRI Ruby 不同的大量扩展或贡献。
Finally Ruby 1.8 yes it's slower because they were executing code on the AST itself so they used to parse the AST multiple times in order to execute the code.
最后 Ruby 1.8 是的,它更慢,因为它们在 AST 本身上执行代码,因此它们曾经多次解析 AST 以执行代码。
If I'm wrong at anything I hope anyone corrects me.
如果我有任何错误,我希望有人纠正我。
Install MRI Ruby dude using RVM or from source. You'll find lots of gems and extensions to work with
使用 RVM 或从源安装 MRI Ruby dude。你会发现很多 gems 和扩展可以使用
回答by Ely
Many users answered already in regards to the benchmarks. Your question is specifically for the usage with Rails.
许多用户已经就基准测试做出了回答。您的问题专门针对 Rails 的使用。
I find it unusual as I don't see clearly the goal of the question. There are many out there using MRI and many using JRuby and many others. I'd go for stability and security and maintainability in that respect, not speed.
我觉得这很不寻常,因为我看不清问题的目标。有很多人在使用 MRI,很多人在使用 JRuby 和许多其他人。在这方面,我会追求稳定性、安全性和可维护性,而不是速度。
However, there is an important difference between MRI and JRuby.
但是,MRI 和 JRuby 之间有一个重要的区别。
MRI uses a GIL. This means, although you can have multiple threads, only one thread can be active at a time. Also implementation of threads can differ in different Ruby versions. Newer versions use OS threads (good step forward), older versions use green threads instead.
MRI 使用 GIL。这意味着,尽管您可以拥有多个线程,但一次只能激活一个线程。线程的实现在不同的 Ruby 版本中也可能不同。新版本使用操作系统线程(向前迈出了一大步),旧版本使用绿色线程代替。
There is no real parallelism in MRI.
MRI 中没有真正的并行性。
MRI is multi-threaded, not thread safe and not parallel.
MRI 是多线程的,不是线程安全的,也不是并行的。
JRuby is multi-threaded, not thread safe and parallel.
JRuby 是多线程的,不是线程安全和并行的。
Parallelism can make a difference in speed. Both are not thread safe, so you will have to take care of that anyway. If you look for speed and can exploit parallelism, then I would give JRuby a definite Yes.
并行性可以在速度上产生差异。两者都不是线程安全的,因此无论如何您都必须照顾它。如果您追求速度并且可以利用并行性,那么我会给 JRuby 一个肯定的答案。
Again, I am not sure if speed is the most important factor when deciding between MRI and JRuby. It is the ecosystem. Since you asked for speed I won't get into this any further.
同样,在决定 MRI 和 JRuby 时,我不确定速度是否是最重要的因素。它是生态系统。既然你要求速度,我就不会再深入了。

