I propose replace all the endless loop do to while true:

NUMBER = 100_000_000

def old_slow
  index = 0
  loop do
    break if index > NUMBER
    index += 1
  end
end

def new_fast
  index = 0
  while true
    break if index > NUMBER
    index += 1
  end
end


Benchmark.ips do |x|
  x.report("new_fast") { new_fast }
  x.report("old_slow") { old_slow }
  x.compare!
end

Calculating -------------------------------------
            new_fast     1.000  i/100ms
            old_slow     1.000  i/100ms
-------------------------------------------------
            new_fast      0.655  (± 0.0%) i/s -      4.000  in   6.106470s
            old_slow      0.259  (± 0.0%) i/s -      2.000  in   7.707258s

Comparison:
            new_fast:        0.7 i/s
            old_slow:        0.3 i/s - 2.52x slower


What do you think about it?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to