Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark/ips'
- Benchmark.ips do |x|
- x.report('mutating and fast') do
- result = ''
- 1.upto(50) do |x|
- result << x
- end
- end
- x.report('pure, but slow') do
- 1.upto(50).reduce('') {|memo, elem| memo + elem.to_s}
- end
- x.compare!
- end
- # Warming up --------------------------------------
- # mutating and fast 18.469k i/100ms
- # pure, but slow 6.683k i/100ms
- # Calculating -------------------------------------
- # mutating and fast 190.909k (± 3.4%) i/s - 960.388k in 5.036743s
- # pure, but slow 68.436k (± 2.3%) i/s - 347.516k in 5.080727s
- # Comparison:
- # mutating and fast: 190908.6 i/s
- # pure, but slow: 68435.5 i/s - 2.79x slower
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement