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:
1008
1008
1009
1009
```
1010
1010
$ 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
+
1015
1013
Calculating -------------------------------------
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
1018
1016
1019
1017
Comparison:
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
1022
1020
```
1023
1021
1024
1022
Original file line number Diff line number Diff line change 1
- require ' benchmark/ips'
1
+ require " benchmark/ips"
2
2
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
5
7
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
7
12
8
13
Benchmark . 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 }
11
16
x . compare!
12
17
end
You can’t perform that action at this time.
0 commit comments