Skip to content

Instantly share code, notes, and snippets.

@blakesmith
Created June 22, 2010 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakesmith/449059 to your computer and use it in GitHub Desktop.
Save blakesmith/449059 to your computer and use it in GitHub Desktop.
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