Sha256: 414a9915eb85784193850919a2a177a8ef430bb22f46c7e6b6b23c2089504f61

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
patternmatching-0.2.4 examples/benchmark.rb