Skip to content

Commit

Permalink
Use methods instead of hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
avellable authored and JuanitoFatas committed Jun 16, 2016
1 parent e012fc9 commit 9cdf609
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1008,17 +1008,15 @@ Comparison:

```
$ ruby -v code/string/mutable_vs_immutable_strings.rb
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
Warming up --------------------------------------
freeze 111.292k i/100ms
normal 106.928k i/100ms
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin14]
Calculating -------------------------------------
freeze 5.282M14.2%) i/s - 25.931M
normal 4.549M14.3%) i/s - 22.348M
Without Freeze 7.279M 6.6%) i/s - 36.451M in 5.029785s
With Freeze 9.329M 7.9%) i/s - 46.370M in 5.001345s
Comparison:
freeze: 5281661.4 i/s
normal: 4548975.9 i/s - same-ish: difference falls within error
With Freeze: 9329054.3 i/s
Without Freeze: 7279203.1 i/s - 1.28x slower
```


Expand Down
17 changes: 11 additions & 6 deletions code/string/mutable_vs_immutable_strings.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
require 'benchmark/ips'
require "benchmark/ips"

IMMUTABLE_TEST = "writing_fast_ruby".freeze
mutable_test = "writing_fast_ruby"
# Allocates new string over and over again
def without_freeze
"To freeze or not to freeze"
end

hash = {"writing_fast_ruby" => "is_cool"}
# Keeps and reuses shared string
def with_feeze
"To freeze or not to freeze".freeze
end

Benchmark.ips do |x|
x.report("freeze") { hash[IMMUTABLE_TEST] }
x.report("normal") { hash[mutable_test] }
x.report("Without Freeze") { without_freeze }
x.report("With Freeze") { with_feeze }
x.compare!
end

0 comments on commit 9cdf609

Please sign in to comment.