Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'benchmark'
class MemberClass
def initialize(a, b, c, d, e, f, g)
@a = a
@b = b
@c = c
@d = d
@e = e
@f = f
@g = g
end
attr_reader :a, :b, :c, :d, :e, :f, :g
end
StructClass = Struct.new(:a, :b, :c, :d, :e, :f, :g)
COUNT = 10000000
Benchmark.bmbm do |x|
x.report("member") do
m = MemberClass.new(0, 0, 0, 0, 0, 0, 0)
COUNT.times do
m.a + m.b + m.c + m.d + m.e + m.f + m.g
end
end
x.report("struct") do
m = StructClass.new(0, 0, 0, 0, 0, 0, 0)
COUNT.times do
m.a + m.b + m.c + m.d + m.e + m.f + m.g
end
end
end
# Rehearsal ------------------------------------------
# member 1.430000 0.000000 1.430000 ( 1.433829)
# struct 1.630000 0.000000 1.630000 ( 1.624923)
# --------------------------------- total: 3.060000sec
#
# user system total real
# member 1.390000 0.000000 1.390000 ( 1.390638)
# struct 1.740000 0.000000 1.740000 ( 1.730032)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.