-
-
Save Narnach/37538 to your computer and use it in GitHub Desktop.
This file contains 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
narnach@narnachbook ~/Development $ jruby speed.rb && ruby speed.rb | |
user system total real | |
define_method :one 0.107740 0.003617 0.111357 ( 0.156973) | |
define_method :two 0.075185 0.001356 0.076541 ( 0.098557) | |
def three 0.033372 0.000635 0.034007 ( 0.042528) | |
user system total real | |
define_method :one 0.100000 0.000000 0.100000 ( 0.130217) | |
define_method :two 0.100000 0.000000 0.100000 ( 0.106536) | |
def three 0.050000 0.000000 0.050000 ( 0.045840) |
This file contains 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
#!/usr/bin/env ruby | |
# | |
# Methods defined using Ruby's define_method are slower to invoke than normally defined (eval-ed) methods. | |
require 'benchmark' | |
module Bench | |
class One | |
def self.add_slow_method(name,&block) | |
define_method(name, &block) | |
end | |
def three | |
3 | |
end | |
end | |
end | |
Bench::One.add_slow_method('one') do | |
1 | |
end | |
Bench::One.add_slow_method('two') do | |
2 | |
end | |
n = 100_000 | |
Benchmark.bm do |x| | |
one = Bench::One.new | |
x.report('define_method :one') do | |
n.times {one.one} | |
end | |
x.report('define_method :two') do | |
n.times {one.two} | |
end | |
x.report('def three') do | |
n.times {one.three} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment