Skip to content

Instantly share code, notes, and snippets.

@joannecheng
Created August 8, 2012 21:06
Show Gist options
  • Save joannecheng/3298792 to your computer and use it in GitHub Desktop.
Save joannecheng/3298792 to your computer and use it in GitHub Desktop.
Some more Jruby notes: compiled java classes vs ruby classes
require 'rubygems'
require 'java'
require 'ackermann'
require 'benchmark'
time = Benchmark.measure do
Ackermann.ack(3, 9)
end
puts "Ruby :\t#{time}"
time = Benchmark.measure do
Java::Ackermann.ack(3, 9)
end
puts "Compiled Java:\t#{time}"
public class Ackermann {
public static int ack(int m, int n){
if (m == 0){
return n + 1;
}
if (n == 0){
return ack(m - 1, 1);
}
return ack(m-1, ack(m, n-1));
}
}
class Ackermann
def self.ack(m, n)
return n + 1 if m == 0
return ack(m-1, 1) if n == 0
return ack(m-1, ack(m, n-1))
end
end
joanne-chengs-MacBook-Pro-2:jruby-tests joannecheng$ jruby ackerizer.rb
Ruby : 0.730000 0.040000 0.770000 ( 0.345000)
Compiled Java: 0.050000 0.000000 0.050000 ( 0.054000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment