Created
July 23, 2014 04:31
-
-
Save holysugar/d2bd2204b9fbf91ea5ba to your computer and use it in GitHub Desktop.
benchmark structs. Struct が速いのは当たり前なので ostruct と hashie の比較がメイン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'benchmark' | |
require 'ostruct' | |
require 'hashie' | |
n = 1000000 | |
O = Struct.new(:foo, :bar, :baz) | |
puts "Benchmark: Initialize" | |
Benchmark.bmbm(9) do |x| | |
x.report('struct: ') { | |
n.times { | |
o = O.new({foo: 1, bar: 2, baz: 3}) | |
} | |
} | |
x.report('ostruct: ') { | |
n.times { | |
o = OpenStruct.new({foo: 1, bar: 2, baz: 3}) | |
} | |
} | |
x.report('hashie: ') { | |
n.times { | |
o = Hashie::Mash.new({foo: 1, bar: 2, baz: 3}) | |
} | |
} | |
end | |
puts | |
puts "Benchmark: Read as []" | |
Benchmark.bmbm(9) do |x| | |
s = O.new({foo: 1, bar: 2, baz: 3}) | |
o = OpenStruct.new({foo: 1, bar: 2, baz: 3}) | |
h = Hashie::Mash.new({foo: 1, bar: 2, baz: 3}) | |
x.report('struct: ') { | |
n.times { | |
foo = s['foo'] | |
} | |
} | |
x.report('ostruct: ') { | |
n.times { | |
foo = o['foo'] | |
} | |
} | |
x.report('hashie: ') { | |
n.times { | |
foo = h['foo'] | |
} | |
} | |
end | |
puts | |
puts "Benchmark: Read as method" | |
Benchmark.bmbm(9) do |x| | |
s = O.new({foo: 1, bar: 2, baz: 3}) | |
o = OpenStruct.new({foo: 1, bar: 2, baz: 3}) | |
h = Hashie::Mash.new({foo: 1, bar: 2, baz: 3}) | |
x.report('struct: ') { | |
n.times { | |
foo = s.foo | |
} | |
} | |
x.report('ostruct: ') { | |
n.times { | |
foo = o.foo | |
} | |
} | |
x.report('hashie: ') { | |
n.times { | |
foo = h.foo | |
} | |
} | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] | |
bench.rb:16: warning: assigned but unused variable - o | |
bench.rb:22: warning: assigned but unused variable - o | |
bench.rb:28: warning: assigned but unused variable - o | |
bench.rb:44: warning: assigned but unused variable - foo | |
bench.rb:50: warning: assigned but unused variable - foo | |
bench.rb:56: warning: assigned but unused variable - foo | |
bench.rb:72: warning: assigned but unused variable - foo | |
bench.rb:78: warning: assigned but unused variable - foo | |
bench.rb:84: warning: assigned but unused variable - foo | |
Benchmark: Initialize | |
Rehearsal --------------------------------------------- | |
struct: 0.710000 0.010000 0.720000 ( 0.719889) | |
ostruct: 13.870000 0.010000 13.880000 ( 13.885477) | |
hashie: 3.950000 0.010000 3.960000 ( 3.950151) | |
----------------------------------- total: 18.560000sec | |
user system total real | |
struct: 0.690000 0.000000 0.690000 ( 0.694495) | |
ostruct: 13.820000 0.020000 13.840000 ( 13.842891) | |
hashie: 3.940000 0.000000 3.940000 ( 3.945889) | |
Benchmark: Read as [] | |
Rehearsal --------------------------------------------- | |
struct: 0.250000 0.000000 0.250000 ( 0.248579) | |
ostruct: 0.250000 0.000000 0.250000 ( 0.250578) | |
hashie: 0.310000 0.000000 0.310000 ( 0.304485) | |
------------------------------------ total: 0.810000sec | |
user system total real | |
struct: 0.240000 0.000000 0.240000 ( 0.248644) | |
ostruct: 0.240000 0.000000 0.240000 ( 0.241594) | |
hashie: 0.310000 0.000000 0.310000 ( 0.312770) | |
Benchmark: Read as method | |
Rehearsal --------------------------------------------- | |
struct: 0.070000 0.000000 0.070000 ( 0.067485) | |
ostruct: 0.140000 0.000000 0.140000 ( 0.142525) | |
hashie: 0.650000 0.000000 0.650000 ( 0.647260) | |
------------------------------------ total: 0.860000sec | |
user system total real | |
struct: 0.070000 0.000000 0.070000 ( 0.067258) | |
ostruct: 0.140000 0.000000 0.140000 ( 0.140880) | |
hashie: 0.640000 0.000000 0.640000 ( 0.639556) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
この initialize は新しいキーを用いたwriteに当たるから当然キーの数で結果変わるよな…そこもパラメータにしないといけない。