Skip to content

Instantly share code, notes, and snippets.

@frsyuki
Created December 13, 2015 08:56
Show Gist options
  • Save frsyuki/df383d94ed56c7128760 to your computer and use it in GitHub Desktop.
Save frsyuki/df383d94ed56c7128760 to your computer and use it in GitHub Desktop.
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
COUNT.times do
m = MemberClass.new(0, 0, 0, 0, 0, 0, 0)
m.a + m.b + m.c + m.d + m.e + m.f + m.g
end
end
x.report("struct") do
COUNT.times do
m = StructClass.new(0, 0, 0, 0, 0, 0, 0)
m.a + m.b + m.c + m.d + m.e + m.f + m.g
end
end
end
# Rehearsal ------------------------------------------
# member 8.870000 0.070000 8.940000 ( 8.988045)
# struct 6.960000 0.070000 7.030000 ( 7.106504)
# -------------------------------- total: 15.970000sec
#
# user system total real
# member 9.140000 0.120000 9.260000 ( 9.596857)
# struct 6.780000 0.070000 6.850000 ( 7.079937)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment