public
Created

  • Download Gist
factorial_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.