Why is the Ruby loop command slower than true?

Ruby has a built-in command loopthat executes the block following it forever (or until it stops at break). However, comparing it with functionally similar while true, it is much slower:

require "benchmark/ips"

NUMBER = 100_000_000

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

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

Benchmark.ips do |x|
  x.report("While Loop")  { fast }
  x.report("Kernel loop") { slow }
  x.compare!
end

In Ruby 2.4.1 (p111 (version 2017-03-22 58053) [x64-mingw32]) the difference is striking:

Warming up --------------------------------------
          While Loop     1.000  i/100ms
         Kernel loop     1.000  i/100ms
Calculating -------------------------------------
          While Loop      0.630  (± 0.0%) i/s -      4.000  in   6.350897s
         Kernel loop      0.190  (± 0.0%) i/s -      1.000  in   5.274249s

Comparison:
          While Loop:        0.6 i/s
         Kernel loop:        0.2 i/s - 3.32x  slower

Why is there such a difference in performance? And why is a single-purpose team loopworse at their job than universal while?

(Benchmark copied from here , licensed under CC-BY-SA)

+4
source share
2 answers

loop - , block. , a block .

:

loop do
 a = 2
 break
end
puts a

, : "NameError: undefined ` a ' main: Object" :

while true
 a = 2
 break
end
p a #=> return a = 2

, loop - (), ​​ break, () . / .

+6

, , , , .

while , , , loop do...end . , , - .

-4

Source: https://habr.com/ru/post/1681312/


All Articles