Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.73 KB | None | 0 0
  1. require 'benchmark/ips'
  2.  
  3.  
  4. Benchmark.ips do |x|
  5.   x.report('mutating and fast') do
  6.     result = ''
  7.     1.upto(50) do |x|
  8.       result << x
  9.     end
  10.   end
  11.  
  12.   x.report('pure, but slow') do
  13.     1.upto(50).reduce('') {|memo, elem| memo + elem.to_s}
  14.   end
  15.  
  16.   x.compare!
  17. end
  18. # Warming up --------------------------------------
  19. #    mutating and fast    18.469k i/100ms
  20. #       pure, but slow     6.683k i/100ms
  21. # Calculating -------------------------------------
  22. #    mutating and fast    190.909k (± 3.4%) i/s -    960.388k in   5.036743s
  23. #       pure, but slow     68.436k (± 2.3%) i/s -    347.516k in   5.080727s
  24.  
  25. # Comparison:
  26. #    mutating and fast:   190908.6 i/s
  27. #       pure, but slow:    68435.5 i/s - 2.79x  slower
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement