require "patternmatching" include PatternMatching class Through def plain(n) n end func(:pattern).seems as {:n} do n end end count = 10000 plain = Through.new start = Time.new count.times do |i| plain.plain(i) end plain_time = Time.new - start pattern = Through.new start = Time.new count.times do |i| pattern.pattern(i) end pattern_time = Time.new - start puts "Check Through call " + count.to_s + " times" puts "Standard Method cost: " + plain_time.to_s + " sec" puts "Partial Method cost: " + pattern_time.to_s + " sec" puts "Overhead/method: " + ((pattern_time - plain_time).to_f / count * 1000).to_s + " msec" puts class Recursive def plain(n) if n > 0 plain(n - 1) end end func(:pattern).seems as {:n}, with {n > 0} do pattern(n-1) end end # size depends on system stack max #count = 400 # for ruby 1.8 stack max #count = 300 # for ruby 1.9 stack max count = 100 # for jruby stack max plain = Recursive.new start = Time.new plain.plain(count) plain_time = Time.new - start pattern = Recursive.new start = Time.new pattern.pattern(count) pattern_time = Time.new - start puts "Check Recursive call " + count.to_s + " times" puts "Standard Method cost: " + plain_time.to_s + " sec" puts "Partial Method cost: " + pattern_time.to_s + " sec" puts "Overhead/method: " + ((pattern_time - plain_time).to_f / count * 1000).to_s + " msec" puts