The case of Ruby, which is better than Java

I am making a presentation about the Ruby ecosystem targeting the Java crowd.

Despite the fact that I will draw conclusions about the performance of time and productivity, but it will be slower still fast enough, it will be good if I can come up with a test that shows that Ruby is superior to Java in a number of crunches, the question is suitable, for fun.


Unfortunately, after several hours of searching and experimenting, I still got nothing.

Can anyone come up with a benchmark that shows that things are not completely black and white?

Ideally, it should compare the latest versions of java 1.6 / 1.8 with the latest version of cruby . Any examples involving rubinius / jruby are also welcome.

+6
source share
3 answers

As a result, I will use a simple example of a regular expression - /(foo|bar)*/ , trying to match 'foobar' * n :


 puts 'Done matching' if ('foobar' * 666).match(/(foo|bar)*/) 

VS


 import java.util.Collections; import java.util.regex.Pattern; import java.util.regex.Matcher; public class SimpleRegexTester { public static void main(String[] args) { String repeatedFooBar = String.join("", Collections.nCopies(666, "foobar")); Pattern p = Pattern.compile("(foo|bar)*"); Matcher m = p.matcher(repeatedFooBar); if (m.matches()) System.out.println("Done matching"); } } 

It is not technically faster, the java version does not work (throws a StackOverflowError ). This IMHO far surpasses the view that java is better optimized for all possible scenarios.

It also gives me a nice transition to talking about code terminology, especially after I show the pre java8 version.

+2
source

I don’t think you will find anything. When it comes to optimization, Ruby and Java are actually very similar, the main point of pain for both is the boxed objects and the dynamic dispatch of a method that they both inherited from Smalltalk (directly Ruby, Java through Objective-C's main inspiration ). And Java virtual machines are simply the most advanced runtimes for dynamically distributed OO languages. For example, there may be some research for Scheme that is even faster, but when it comes to industrial-ready industrial implementations, Azul Zing , Oracle HotSpot , Oracle JRockit , IBM J9 and friends win hands down.

So, if the guys from Rubinius didn't come up with something that the Smalltalk / Java community didn't notice, you'll pretty much find yourself at the same performance at best.

Your best bet is not digital processing, but word processing. This is what pleases the legacy of Ruby Perl. Most regex mechanisms of Java implementations are not very efficient (although they are improving), while Onigmo is actually not bad (not as good as Perl, though). In addition, independent Ruby character-encoded strings prevent re-encoding of your string, whereas Java strings should always be encoded in UTF-16 and from UTF-16 if the input and output encodings are not UTF-16, which is unlikely. In Ruby, you need to transcode no more than once, even if your input and output encoding is different, you can set the internal encoding to be the same as input or output encoding, and therefore you only need to transcode during input or output, but not both .

There are examples, however, of Ruby competing with C, and since everyone knows and trades; that C is faster than Java, it certainly should mean that Ruby is faster than Java, right? Right?

[In fact, finding examples where Ruby outperforms C is probably simpler because dynamic optimizations such as speculative inline, polymorphic inline caching, adaptive optimizations, and the “unsafe” optimizations provided by dynamic de-optimizations do not exist in typical C implementations.]

In particular, the Rubinius Hash class , which is written in Ruby, is significantly slower than the YARV Hash class , which is written in C.

And a really exciting example: JRuby + Truffle + Graal + TruffleC can run YARV C extensions in the C interpreter on top of the JVM (!!!) faster than YARV can run C extensions initially:

[Of course, this last example is actually an example of the strength of Truffle and Graal , i.e. two Java technologies, more than Ruby's power example.]

+14
source

I once attended a conversation with a JRuby developer who pointed out that they get better tests from JRuby than from their native Ruby, and that is because Sun / Oracle spent a lot of money making Hotspot incredibly good at optimizing JIT.

I suspect you might find some tests in which JRuby will be as fast as plain Java as soon as JIT can do its magic.

So, try the server application, perhaps a web application, run it and run the code at least once before starting to measure performance. This should mean that a JIT has occurred, and you may find that performance is equivalent to plain Java.

However, the tractor is not a racing car. One wins races well, the other is good at pulling out plows. There are also reasons why more than one language exists.

0
source

All Articles