Closed avellable closed 8 years ago
So, you created two String
objects, and compare how fast Hash#[]
works with strings... o_O
What really makes sense in terms of immutable strings is:
# allocates new string over and over again
def slow
"foobar"
end
# keeps and reuses shared string
def fast
"foobar".freeze
end
Benchmark.ips do |x|
x.report("slow") { slow }
x.report("fast") { fast }
x.compare!
end
What @ixti said :+1:
Yeah, looks like that makes more sense.
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
Warming up --------------------------------------
Without Freeze 106.961k i/100ms
With Freeze 118.210k i/100ms
Calculating -------------------------------------
Without Freeze 4.386M (± 6.1%) i/s - 21.927M
With Freeze 6.893M (± 6.9%) i/s - 34.281M
Comparison:
With Freeze: 6892639.5 i/s
Without Freeze: 4386460.4 i/s - 1.57x slower
I updated the PR.
@ixti @nateberkopec Thanks for review 🙇
@avellable Would you like to be a collaborator :) ?
@JuanitoFatas Sure!
@JuanitoFatas Sure!
Added, welcome 😃
Thank you. :)
Using 'freeze' increases performances for strings.
Source: http://tmm1.net/ruby21-fstrings/