Created
June 22, 2010 20:57
-
-
Save blakesmith/449059 to your computer and use it in GitHub Desktop.
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 'inline' | |
class MyTestInC | |
inline do |builder| | |
builder.c <<-EOF | |
long factorial(int max) { | |
int i=max, result=1; | |
while (i >= 2) { | |
result *= i--; | |
} | |
return result; | |
} | |
EOF | |
end | |
end | |
class MyTestInRuby | |
def factorial(n) | |
return 1 if n == 0 | |
n * factorial(n-1) | |
end | |
end | |
def time_test(test_name, times=10000, &block) | |
start = Time.now | |
if block_given? | |
times.times { yield } | |
puts "Called block '#{test_name}' #{times} times in #{Time.now - start} seconds" | |
end | |
end | |
c = MyTestInC.new | |
r = MyTestInRuby.new | |
time_test "With C Extension" do | |
c.factorial(10) | |
end | |
time_test "In Pure Ruby" do | |
r.factorial(10) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment