-
-
Save RyanScottLewis/383882 to your computer and use it in GitHub Desktop.
RuleBook benchmarks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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