Skip to content

Instantly share code, notes, and snippets.

@craigw
Created March 20, 2014 12:04
Show Gist options
  • Save craigw/9662292 to your computer and use it in GitHub Desktop.
Save craigw/9662292 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Foo
attr_accessor :a
def a
@a = a
end
end
class Bar < Struct.new(:b)
end
def build c
c.new
end
n = 10_000_000
Benchmark.bmbm do |x|
x.report("Class") { n.times { build(Foo) } }
x.report("Struct") { n.times { build(Bar) } }
end
__END__
Rehearsal ------------------------------------------
Class 1.760000 0.000000 1.760000 ( 1.763450)
Struct 2.570000 0.000000 2.570000 ( 2.574691)
--------------------------------- total: 4.330000sec
user system total real
Class 1.840000 0.000000 1.840000 ( 1.845105)
Struct 2.720000 0.010000 2.730000 ( 2.736802)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment