Skip to content

Commit 9cdf609

Browse files
avellableJuanitoFatas
authored andcommitted
Use methods instead of hash.
Closes #100
1 parent e012fc9 commit 9cdf609

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff 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+
10151013
Calculating -------------------------------------
1016-
freeze 5.282M14.2%) i/s - 25.931M
1017-
normal 4.549M14.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
10191017
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
10221020
```
10231021

10241022

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
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

813
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 }
1116
x.compare!
1217
end

0 commit comments

Comments
 (0)