Created
October 18, 2011 03:08
-
-
Save sferik/1294519 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 "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