Created
April 29, 2010 16:15
-
-
Save burke/383832 to your computer and use it in GitHub Desktop.
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?}}}" | |
# method defined with define_method: 0.0745489597320557 | |
# method defined with rulebook: 1.54464817047119 | |
# method defined by hand (with def): 0.0245590209960938 | |
# method defined with eval (with def): 0.02919602394104 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm. I havn't had a chance to run benchmarks yet.
Maybe caching the rules would make a difference.. I'll work on this.
If you have any idea's, feel free to help out. It would be greatly appreciated.