Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require "benchmark/ips"
puts RUBY_DESCRIPTION
class Inherit < Struct.new(:id, :name, :age)
def name_and_age
"#{name}, #{age}"
end
end
Block = Struct.new(:id, :name, :age) do
def name_and_age
"#{name}, #{age}"
end
end
Reopen = Struct.new(:id, :name, :age)
class Reopen
def name_and_age
"#{name}, #{age}"
end
end
Benchmark.ips do |x|
x.report("Inherit"){ |times|
i = 0
while i < times
Inherit.new(1, 2, 3).name_and_age
i += 1
end
}
x.report("Block"){ |times|
i = 0
while i < times
Block.new(1, 2, 3).name_and_age
i += 1
end
}
x.report("Reopen"){ |times|
i = 0
while i < times
Reopen.new(1, 2, 3).name_and_age
i += 1
end
}
x.compare!
end
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
Calculating -------------------------------------
Inherit 52.839k i/100ms
Block 52.457k i/100ms
Reopen 52.828k i/100ms
-------------------------------------------------
Inherit 1.152M (± 3.6%) i/s - 5.759M
Block 1.200M (± 2.4%) i/s - 6.033M
Reopen 1.203M (± 0.9%) i/s - 6.022M
Comparison:
Reopen: 1202725.9 i/s
Block: 1199646.4 i/s - 1.00x slower
Inherit: 1151912.6 i/s - 1.04x slower
jruby 9.0.0.0-SNAPSHOT (2.2.2) 2015-05-04 6055b79 Java HotSpot(TM) 64-Bit Server VM 24.80-b11 on 1.7.0_80-b15 +indy +jit [linux-amd64]
Calculating -------------------------------------
Inherit 24.651k i/100ms
Block 52.973k i/100ms
Reopen 55.793k i/100ms
-------------------------------------------------
Inherit 1.366M (±11.2%) i/s - 6.656M
Block 1.436M (± 9.4%) i/s - 7.098M
Reopen 1.464M (± 8.2%) i/s - 7.253M
Comparison:
Reopen: 1463500.4 i/s
Block: 1436473.0 i/s - 1.02x slower
Inherit: 1365655.1 i/s - 1.07x slower
rubinius 2.5.3 (2.1.0 2482b093 2015-04-28 3.4 JI) [x86_64-linux-gnu]
Calculating -------------------------------------
Inherit 12.337k i/100ms
Block 18.838k i/100ms
Reopen 18.643k i/100ms
-------------------------------------------------
Inherit 231.421k (± 3.3%) i/s - 1.160M
Block 241.499k (± 1.4%) i/s - 1.224M
Reopen 242.887k (± 1.4%) i/s - 1.230M
Comparison:
Reopen: 242886.6 i/s
Block: 241499.1 i/s - 1.01x slower
Inherit: 231420.7 i/s - 1.05x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment