Skip to content

Instantly share code, notes, and snippets.

@samaaron
Created December 18, 2008 15:31
Show Gist options
  • Save samaaron/37529 to your computer and use it in GitHub Desktop.
Save samaaron/37529 to your computer and use it in GitHub Desktop.
λ jruby speed.rb
user system total real
define_method :one 0.408000 0.000000 0.408000 ( 0.407368)
define_method :two 0.123000 0.000000 0.123000 ( 0.122547)
def three 0.078000 0.000000 0.078000 ( 0.077790)
∴ /Users/sam/Desktop
λ ruby speed.rb
user system total real
define_method :one 0.580000 0.240000 0.820000 ( 0.848865)
define_method :two 0.580000 0.250000 0.830000 ( 0.842408)
def three 0.350000 0.160000 0.510000 ( 0.525318)
#!/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