File tree Expand file tree Collapse file tree 2 files changed +17
-14
lines changed Expand file tree Collapse file tree 2 files changed +17
-14
lines changed Original file line number Diff line number Diff line change @@ -1008,17 +1008,15 @@ Comparison:
10081008
10091009```
10101010$ ruby -v code/string/mutable_vs_immutable_strings.rb
1011- ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
1012- Warming up --------------------------------------
1013- freeze 111.292k i/100ms
1014- normal 106.928k i/100ms
1011+ ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin14]
1012+
10151013Calculating -------------------------------------
1016- freeze 5.282M (±14.2 %) i/s - 25.931M
1017- normal 4.549M (±14.3 %) i/s - 22.348M
1014+ Without Freeze 7.279M (± 6.6 %) i/s - 36.451M in 5.029785s
1015+ With Freeze 9.329M (± 7.9 %) i/s - 46.370M in 5.001345s
10181016
10191017Comparison:
1020- freeze : 5281661.4 i/s
1021- normal : 4548975.9 i/s - same-ish: difference falls within error
1018+ With Freeze : 9329054.3 i/s
1019+ Without Freeze : 7279203.1 i/s - 1.28x slower
10221020```
10231021
10241022
Original file line number Diff line number Diff line change 1- require ' benchmark/ips'
1+ require " benchmark/ips"
22
3- IMMUTABLE_TEST = "writing_fast_ruby" . freeze
4- mutable_test = "writing_fast_ruby"
3+ # Allocates new string over and over again
4+ def without_freeze
5+ "To freeze or not to freeze"
6+ end
57
6- hash = { "writing_fast_ruby" => "is_cool" }
8+ # Keeps and reuses shared string
9+ def with_feeze
10+ "To freeze or not to freeze" . freeze
11+ end
712
813Benchmark . ips do |x |
9- x . report ( "freeze " ) { hash [ IMMUTABLE_TEST ] }
10- x . report ( "normal " ) { hash [ mutable_test ] }
14+ x . report ( "Without Freeze " ) { without_freeze }
15+ x . report ( "With Freeze " ) { with_feeze }
1116 x . compare!
1217end
You can’t perform that action at this time.
0 commit comments