Created
August 8, 2012 21:06
-
-
Save joannecheng/3298792 to your computer and use it in GitHub Desktop.
Some more Jruby notes: compiled java classes vs ruby classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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