Skip to content

Instantly share code, notes, and snippets.

@dominikh
Created October 12, 2009 01:22
Show Gist options
  • Save dominikh/208034 to your computer and use it in GitHub Desktop.
Save dominikh/208034 to your computer and use it in GitHub Desktop.
class A < Struct.new(:a, :b)
end
B = Struct.new(:a, :b)
require 'benchmark'
Benchmark.bmbm 20 do |r|
n = 10_000_000
a = A.new(1,2)
b = B.new(1,2)
r.report('initialize inherited Struct'){ n.times{ A.new(1,2) } }
r.report('initialize assigned Struct'){ n.times{ B.new(1,2) } }
r.report('access inherited Struct'){ n.times{ a.a; a.b } }
r.report('access assigned Struct'){ n.times{ b.a; b.b } }
end
# Rehearsal ---------------------------------------------------------------
# initialize inherited Struct 22.610000 0.030000 22.640000 ( 22.762717)
# initialize assigned Struct 21.640000 0.050000 21.690000 ( 21.816132)
# access inherited Struct 5.390000 0.000000 5.390000 ( 5.410279)
# access assigned Struct 5.380000 0.020000 5.400000 ( 5.416878)
# ----------------------------------------------------- total: 55.120000sec
# user system total real
# initialize inherited Struct 22.630000 0.030000 22.660000 ( 22.669520)
# initialize assigned Struct 22.830000 0.040000 22.870000 ( 22.878188)
# access inherited Struct 5.450000 0.010000 5.460000 ( 5.471413)
# access assigned Struct 5.750000 0.010000 5.760000 ( 5.766549)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment