Skip to content

Instantly share code, notes, and snippets.

@wesmaldonado
Created July 30, 2010 23:40
Show Gist options
  • Save wesmaldonado/501502 to your computer and use it in GitHub Desktop.
Save wesmaldonado/501502 to your computer and use it in GitHub Desktop.
# Shows the difference in doing crazy stuff like extending an instance on a say... busy loop or lots of requests.
#
# [16:38][wmaldonado@wmaldonado0:]$ ruby test.local.extend.rb
# user system total real
# extend instance 23.150000 0.430000 23.580000 ( 23.777088)
# create a class 2.390000 0.810000 3.200000 ( 3.209350)
#
require 'benchmark'
module SomethingToExtendStringWith
[:one, :two, :three, :four, :five, :six].each do |mname|
define_method("something_#{mname}") do
end
end
end
class SomethingClass
def initialize(what)
@what = what
end
include SomethingToExtendStringWith
end
n = 1000000
Benchmark.bm do |bm|
bm.report("extend instance"){
n.times do
str = ""
str.extend SomethingToExtendStringWith
end
}
bm.report("create a class") {
n.times do
str = ""
parser = SomethingClass.new(str)
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment