Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Forked from burke/gist:383832
Created April 29, 2010 16:58
Show Gist options
  • Save RyanScottLewis/383882 to your computer and use it in GitHub Desktop.
Save RyanScottLewis/383882 to your computer and use it in GitHub Desktop.
RuleBook benchmarks
require 'rubygems'
require 'benchmark'
require 'rulebook'
class Foo
[:admin, :blah].each do |role|
define_method "is_#{role}?" do
true
end
end
end
class Bar
rule /is_(admin|blah)\?/ do |role|
true
end
end
class Baz
def is_admin?
true
end
def is_blah?
true
end
end
class Quux
[:admin, :blah].each do |role|
eval <<-EOC
def is_#{role}?
true
end
EOC
end
end
foo = Foo.new
bar = Bar.new
baz = Baz.new
quux = Quux.new
puts "method defined with define_method: #{Benchmark.realtime{100_000.times{foo.is_admin?}}}"
puts "method defined with rulebook: #{Benchmark.realtime{100_000.times{bar.is_admin?}}}"
puts "method defined by hand (with def): #{Benchmark.realtime{100_000.times{baz.is_admin?}}}"
puts "method defined with eval (with def): #{Benchmark.realtime{100_000.times{quux.is_admin?}}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment