Skip to content

Instantly share code, notes, and snippets.

@sferik
Created October 18, 2011 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sferik/1294519 to your computer and use it in GitHub Desktop.
Save sferik/1294519 to your computer and use it in GitHub Desktop.
require "rubygems"
require "rbench"
class A
def initialize(hash={})
hash.each do |key, value|
instance_variable_set(:"@#{key}", value)
end
end
end
class B < A
def initialize(hash={})
super(hash)
end
end
class C < A
def initialize(hash={})
@a = hash[:a]
@b = hash[:b]
@c = hash[:c]
end
end
def base
A.new({:a => 1, :b => 2, :c => 3})
end
def with_super
B.new({:a => 1, :b => 2, :c => 3})
end
def without_super
C.new({:a => 1, :b => 2, :c => 3})
end
RBench.run(100_000) do
column :one, :title => 'Base class'
column :two, :title => 'Child class with super'
column :three, :title => 'Child class without super'
report "Which is fastest?" do
one{base}
two{with_super}
three{without_super}
end
end
__END__
Base class | Child class with super | Child class without super |
-------------------------------------------------------------------------------------------
Which is fastest? 0.912 | 0.955 | 0.392 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment