File tree Expand file tree Collapse file tree 3 files changed +71
-0
lines changed Expand file tree Collapse file tree 3 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -225,6 +225,42 @@ allows one to define and raise exceptions with predefined messages.
225225
226226```
227227
228+ ##### Hash vs OpenStruct on access assuming you already have a Hash or an OpenStruct [ code] ( code/general/hash-vs-openstruct-on-access.rb )
229+
230+ ```
231+ $ ruby -v code/general/hash-vs-openstruct-on-access.rb
232+ ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
233+
234+ Calculating -------------------------------------
235+ Hash 128.344k i/100ms
236+ OpenStruct 110.723k i/100ms
237+ -------------------------------------------------
238+ Hash 5.279M (± 7.0%) i/s - 26.311M
239+ OpenStruct 3.048M (± 7.0%) i/s - 15.169M
240+
241+ Comparison:
242+ Hash: 5278844.0 i/s
243+ OpenStruct: 3048139.8 i/s - 1.73x slower
244+ ```
245+
246+ ##### Hash vs OpenStruct (creation) [ code] ( code/general/hash-vs-openstruct.rb )
247+
248+ ```
249+ $ ruby -v code/general/hash-vs-openstruct.rb
250+ ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
251+
252+ Calculating -------------------------------------
253+ Hash 75.510k i/100ms
254+ OpenStruct 9.126k i/100ms
255+ -------------------------------------------------
256+ Hash 1.604M (±11.0%) i/s - 7.929M
257+ OpenStruct 96.855k (± 9.9%) i/s - 483.678k
258+
259+ Comparison:
260+ Hash: 1604259.1 i/s
261+ OpenStruct: 96855.3 i/s - 16.56x slower
262+ ```
263+
228264### Array
229265
230266##### ` Array#bsearch ` vs ` Array#find ` [ code] ( code/array/bsearch-vs-find.rb )
Original file line number Diff line number Diff line change 1+ require "benchmark/ips"
2+ require "ostruct"
3+
4+ HASH = { field_1 : 1 , field_2 : 2 }
5+ OPENSTRUCT = OpenStruct . new ( field_1 : 1 , field_2 : 2 )
6+
7+ def fast
8+ [ HASH [ :field_1 ] , HASH [ :field_2 ] ]
9+ end
10+
11+ def slow
12+ [ OPENSTRUCT . field_1 , OPENSTRUCT . field_2 ]
13+ end
14+
15+ Benchmark . ips do |x |
16+ x . report ( "Hash" ) { fast }
17+ x . report ( "OpenStruct" ) { slow }
18+ x . compare!
19+ end
Original file line number Diff line number Diff line change 1+ require "benchmark/ips"
2+ require "ostruct"
3+
4+ def fast
5+ { field_1 : 1 , field_2 : 2 }
6+ end
7+
8+ def slow
9+ OpenStruct . new ( field_1 : 1 , field_2 : 2 )
10+ end
11+
12+ Benchmark . ips do |x |
13+ x . report ( "Hash" ) { fast }
14+ x . report ( "OpenStruct" ) { slow }
15+ x . compare!
16+ end
You can’t perform that action at this time.
0 commit comments