I have tested recursive method execution speed with classic example of Honoi Tower.
First in Java than JRuby and Ruby with different no. of plates:
package com.example;
public class Hanoi {
public static void main(String[] args) {
int [] plates = {25, 26, 27, 28, 29, 30, 31, 32};
for(int i = 0; i < plates.length; i++){
long start = System.currentTimeMillis();
doTowers(plates[i], 'A', 'B', 'C');
System.out.println(System.currentTimeMillis() - start);
}
}
public static void doTowers(int topN, char from, char inter, char to) {
if (topN == 1) {
//NOP
} else {
doTowers(topN - 1, from, to, inter);
doTowers(topN - 1, inter, from, to);
}
}
}
And results are:
Java(millis) JRuby(sec) Ruby(sec) Ruby(sec) Ruby(sec)
java 7 jruby-1.7.9 jruby-1.7.9 ruby-2.1.3 ruby-2.1.3 {tailcall_optimization: true}
364 0.269 3.395 6.160 5.515
380 0.321 6.288 12.401 11.082
1349 1.173 13.462 25.497 22.661
2328 1.25 25.714 50.223 44.494
4674 4.73 51.159 101.825 89.22
4995 5.014 103.252 200.308 177.034
18633 18.637 208.356 411.667 357.561
19978 20.927 421.86 805.138 711.872
Looks like running on java and jruby has the same performance.
EDITED
Added results of Ruby 2.1.3 with { tailcall_optimization: true }. As you can see now it faster then with default false option.
And another question:
The Ruby and JRuby implementetion are below:
class Hanoi
def do_towers(top_n, from, inter, to)
if top_n == 1
#NOP
else
do_towers top_n - 1, from, to, inter
do_towers top_n - 1, inter, from, to
end
end
end
[25, 26, 27, 28, 29, 30, 31, 32].each do |plate|
start = Time.now
HanoiRb.new.do_towers plate, 'A', 'B', 'C'
puts Time.now - start
end
JRuby:
include Java
$CLASSPATH << 'lib'
Hanoi = JavaUtilities.get_proxy_class('com.example.Hanoi')
[25, 26, 27, 28, 29, 30, 31, 32].each do |plate|
start = Time.now
Hanoi.doTowers(plate, 'A'.to_java.toCharArray[0], 'B'.to_java.toCharArray[0], 'C'.to_java.toCharArray[0])
puts Time.now - start
end
Ruby is simpler hence faster than Java. The code written in Ruby changes on the fly, while its competitor needs to generate the byte code before it can run. The Ruby performance supremacy is true for small tasks only, while Java recoups huge computational needs.
If we consider Ruby vs Java performance, Java brings better application performance. Java code execution is quicker because of transformation into machine language, and the Java Virtual machine creates the code quicker. Java is a well-known language, and it's easier to find a solution to your specific challenge.
Ruby is an interpreted scripting language, whereas Java is a compiled programming language. Ruby is similar to Java in that both are object-oriented languages and are strongly typed. But, Ruby is dynamically typed, whereas Java is statically typed.
Differences between Java and Ruby : Java is a high-level, open-source, object-oriented, and general-purpose programming language. Ruby is a high level, purely1990sa fewer, object-oriented, and general purpose programming language. Java is considered as both compiled and interpreted programming language.
Is it all about JVM?
that is what you results suggest. The JVM does heavily optimise the code.
Or does ruby use only single core of machine?
Your Java program appears to only use one core as well, so that wouldn't matter.
What is the reason for this nonlinear performance loose in Ruby?
The performance in Ruby looks linear with the amount of work required to move all the plates. The Java ones are more surprising.
The JVM doesn't do tail call optimisation so it would be interesting if you did this in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With