# While vs loop

**URL:** <https://rubytalk.org/t/while-vs-loop/51867>\
**Category:** ruby-talk\
**Created:** [20 February 2009 21:57 UTC](https://rubytalk.org/t/while-vs-loop/51867 "2009-02-20T21:57:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Louis-Philippe](https://avatars.discourse-cdn.com/v4/letter/l/db5fbb/32.png) [@Louis-Philippe](https://rubytalk.org/u/Louis-Philippe)\
**Post date:** [20 February 2009 21:57 UTC](https://rubytalk.org/t/while-vs-loop/51867/1 "2009-02-20T21:57:55Z")

</div>

Hi,I saw this thread from last week: while vs loop,

I don't think anybody mentioned the slight performance difference between  
the two,  
(or maybe I got the wrong info!!)  
In fact, I had always assumed the 'loop do' construct to be better than  
'while 1 do', if only to be more elegant or concise for implementing  
infinite loops... but it turns out it seems to be the other way:

> **···**
>
> ##############  
> require 'benchmark'  
> Benchmark.bm do |x|  
> x.report('while1') do  
> n = 0  
> while 1 do  
> break if n \>= 10000000  
> n += 1  
> end  
> end  
> x.report('loop ') do  
> n = 0  
> loop do  
> break if n \>= 10000000  
> n += 1  
> end  
> end  
> end
> 
> ##############  
> RESULTS:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> while 1 19.770000 0.230000 20.000000 ( 34.458926)  
> loop 25.870000 0.300000 26.170000 ( 42.804569)
> 
> ###########
> 
> I'm running this benchmark on 1.8.6 patchlevel 114 (universal-darwin9.0)
> 
> So why is it behaving this way?  
> Are every implementations doing the same?
> 
> Thanks!
> 
> L-P
> 
> On Sat, Feb 14, 2009 at 7:16 AM, Nobuyoshi Nakada \<n...\<[http://groups.google.com/groups/unlock?\_done=/group/ruby-talk-google/browse\_thread/thread/3ee74ef131ec90ba&msg=2f988b0deb69e61d&gt](http://groups.google.com/groups/unlock?_done=/group/ruby-talk-google/browse_thread/thread/3ee74ef131ec90ba&msg=2f988b0deb69e61d&gt); @ruby-lang.org\> wrote:
> 
> > Hi,
> 
> > At Sat, 14 Feb 2009 15:03:03 +0900,  
> > Vetrivel Vetrivel wrote in [ruby-talk:328175]:
> > 
> > > what is the Difference between loop and while in ruby ?
> 
> > > loop do  
> > > end
> 
> > loop is a kernel method which takes a block. A block  
> > introduces new local variable scope.
> 
> > loop do  
> > &nbsp;&nbsp;&nbsp;a = 1  
> > &nbsp;&nbsp;&nbsp;break  
> > end  
> > p a #=\> causes NameError
> 
> > > while 1  
> > > end
> 
> > while doesn't.
> 
> > while 1  
> > &nbsp;&nbsp;&nbsp;a = 1  
> > &nbsp;&nbsp;&nbsp;break  
> > end  
> > p a #=\> 1
> 
> > --  
> > Nobu Nakada
> 
> furthermore loop do has an implicit rescue clause for a StopIteration  
> exception  
> (I believe 1.8.7 and 1.9.1 only IIRC)
> 
> therefore
> 
> loop do  
> &nbsp;&nbsp;&nbsp;some\_enumerator.next  
> end  
> becomes a convenient idiom.
> 
> HTH  
> Robert

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [20 February 2009 22:21 UTC](https://rubytalk.org/t/while-vs-loop/51867/2 "2009-02-20T22:21:34Z")

</div>

> Hi,I saw this thread from last week: while vs loop,
> 
> I don't think anybody mentioned the slight performance difference between  
> the two,

If you're going to talk performance differences between while and loop, don't forget for and each:

> % ./x.rb 1000000  
> # of iterations = 1000000  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> each 0.150000 0.000000 0.150000 ( 0.148313)  
> for 0.170000 0.000000 0.170000 ( 0.173150)  
> each-var 0.230000 0.000000 0.230000 ( 0.237410)  
> while 0.640000 0.010000 0.650000 ( 0.644029)  
> loop 0.780000 0.000000 0.780000 ( 0.785485)  
> % ./x.rb 10000000  
> # of iterations = 10000000  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> each 1.370000 0.010000 1.380000 ( 1.380012)  
> for 1.630000 0.000000 1.630000 ( 1.652562)  
> each-var 2.450000 0.010000 2.460000 ( 2.479478)  
> while 5.510000 0.010000 5.520000 ( 5.532909)  
> loop 7.730000 0.020000 7.750000 ( 7.786959)

require 'benchmark'

max = (ARGV.shift || 1\_000\_000).to\_i

puts "# of iterations = #{max}"  
Benchmark::bm(20) do |x|  
&nbsp;&nbsp;&nbsp;x.report("each") do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(0..max).each do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;x.report("for") do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in 0..max do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do nothing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;x.report("each-var") do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(0..max).each do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;x.report("while") do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while true do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if n \>= max  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n += 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;x.report("loop") do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if n \>= max  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n += 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> On Feb 20, 2009, at 13:57 , Louis-Philippe wrote:

---

<div class="post-metadata">

**Author:** ![Louis-Philippe](https://avatars.discourse-cdn.com/v4/letter/l/db5fbb/32.png) [@Louis-Philippe](https://rubytalk.org/u/Louis-Philippe)\
**Post date:** [20 February 2009 23:02 UTC](https://rubytalk.org/t/while-vs-loop/51867/3 "2009-02-20T23:02:56Z")

</div>

mmm, Interesting to see loop being this high in the list,  
but even if they all loop from 0 to X doesn't mean they can all compared...  
I'm not sure what was the initial question (!!!), but mine is about infinite  
loop implementations,  
where list iterators of the .each flavour don't do.

So \*THE\* fastest ruby infinite loop is 'while 1'?

> **···**
>
> 2009/2/20 Ryan Davis \<ryand-ruby@zenspider.com\>
> 
> > On Feb 20, 2009, at 13:57 , Louis-Philippe wrote:
> > 
> > Hi,I saw this thread from last week: while vs loop,
> > 
> > > I don't think anybody mentioned the slight performance difference between  
> > > the two,
> > 
> > If you're going to talk performance differences between while and loop,  
> > don't forget for and each:
> > 
> > % ./x.rb 1000000
> > 
> > > # of iterations = 1000000  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > > each 0.150000 0.000000 0.150000 ( 0.148313)  
> > > for 0.170000 0.000000 0.170000 ( 0.173150)  
> > > each-var 0.230000 0.000000 0.230000 ( 0.237410)  
> > > while 0.640000 0.010000 0.650000 ( 0.644029)  
> > > loop 0.780000 0.000000 0.780000 ( 0.785485)  
> > > % ./x.rb 10000000  
> > > # of iterations = 10000000  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > > each 1.370000 0.010000 1.380000 ( 1.380012)  
> > > for 1.630000 0.000000 1.630000 ( 1.652562)  
> > > each-var 2.450000 0.010000 2.460000 ( 2.479478)  
> > > while 5.510000 0.010000 5.520000 ( 5.532909)  
> > > loop 7.730000 0.020000 7.750000 ( 7.786959)
> > 
> > require 'benchmark'
> > 
> > max = (ARGV.shift || 1\_000\_000).to\_i
> > 
> > puts "# of iterations = #{max}"  
> > Benchmark::bm(20) do |x|  
> > x.report("each") do  
> > &nbsp;&nbsp;&nbsp;(0..max).each do  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > x.report("for") do  
> > &nbsp;&nbsp;&nbsp;for i in 0..max do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do nothing  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > x.report("each-var") do  
> > &nbsp;&nbsp;&nbsp;(0..max).each do |i|  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > x.report("while") do  
> > &nbsp;&nbsp;&nbsp;n = 0  
> > &nbsp;&nbsp;&nbsp;while true do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if n \>= max  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n += 1  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > x.report("loop") do  
> > &nbsp;&nbsp;&nbsp;n = 0  
> > &nbsp;&nbsp;&nbsp;loop do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if n \>= max
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n += 1  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > end

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [21 February 2009 08:50 UTC](https://rubytalk.org/t/while-vs-loop/51867/4 "2009-02-21T08:50:43Z")

</div>

> mmm, Interesting to see loop being this high in the list,  
> but even if they all loop from 0 to X doesn't mean they can all compared...  
> I'm not sure what was the initial question (!!!), but mine is about infinite  
> loop implementations,  
> where list iterators of the .each flavour don't do.
> 
> So \*THE\* fastest ruby infinite loop is 'while 1'?

wrong

> **···**
>
> On Feb 20, 2009, at 15:02 , Louis-Philippe wrote:
> 
> > \>\> Infinity = 1 / 0.0  
> > =\> Infinity  
> > \>\> (0..Infinity).each {}

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [21 February 2009 14:42 UTC](https://rubytalk.org/t/while-vs-loop/51867/5 "2009-02-21T14:42:57Z")

</div>

Anything purporting to be an infinite loop which runs faster than something  
which is an infinite loop is, by definition NOT an infinite loop!  
\<G\>

> **···**
>
> On Fri, Feb 20, 2009 at 6:02 PM, Louis-Philippe \<default@spiralix.org\>wrote:
> 
> > mmm, Interesting to see loop being this high in the list,  
> > but even if they all loop from 0 to X doesn't mean they can all  
> > compared...  
> > I'm not sure what was the initial question (!!!), but mine is about  
> > infinite  
> > loop implementations,  
> > where list iterators of the .each flavour don't do.
> > 
> > So \*THE\* fastest ruby infinite loop is 'while 1'?
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Jeff\_Schwab](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@Jeff\_Schwab](https://rubytalk.org/u/Jeff_Schwab)\
**Post date:** [21 February 2009 15:49 UTC](https://rubytalk.org/t/while-vs-loop/51867/6 "2009-02-21T15:49:41Z")

</div>

Rick DeNatale wrote:

> Anything purporting to be an infinite loop which runs faster than something  
> which is an infinite loop is, by definition NOT an infinite loop!  
> \<G\>

Not sure Georg Cantor would agree with you. One infinite loop might only go through a countable (but infinite) number of iterations, i.e. aleph null, whereas another might go through an uncountable number of iterations.

Suppose one loop iterates over just the rationals, while another iterates over the reals. The former iterates fewer times than the latter, even though both are infinite.

Still, your point is well taken. 🙂

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [21 February 2009 16:54 UTC](https://rubytalk.org/t/while-vs-loop/51867/7 "2009-02-21T16:54:57Z")

</div>

This was one of the most famous stories in our department . We were  
developing a Debugger and my colleague was debugging an infinite loop,  
which happened to be inside another infinite loop. Of course he did  
not discover the problem of the outer infinite loop before having  
fixed the inner one.  
When our project leader asked him about the progress he had made, he  
genuinely replied: "It still loops, but much faster". We happened to  
pull his leg for quite some time of course :)?  
Robert

> **···**
>
> On Sat, Feb 21, 2009 at 3:42 PM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:
> 
> > On Fri, Feb 20, 2009 at 6:02 PM, Louis-Philippe \<default@spiralix.org\>wrote:
> 
> > Anything purporting to be an infinite loop which runs faster than something  
> > which is an infinite loop is, by definition NOT an infinite loop!  
> > \<G\>
> 
> --  
> There are some people who begin the Zoo at the beginning, called  
> WAYIN, and walk as quickly as they can past every cage until they get  
> to the one called WAYOUT, but the nicest people go straight to the  
> animal they love the most, and stay there. ~ A.A. Milne (from  
> Winnie-the-Pooh)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [21 February 2009 16:56 UTC](https://rubytalk.org/t/while-vs-loop/51867/8 "2009-02-21T16:56:39Z")

</div>

Might it? I do not think so! I believe that a Turing complete language  
can only loop countable times, which of course should be fixed ;).  
R.

> **···**
>
> On Sat, Feb 21, 2009 at 4:49 PM, Jeff Schwab \<jeff@schwabcenter.com\> wrote:
> 
> > Rick DeNatale wrote:
> > 
> > > Anything purporting to be an infinite loop which runs faster than  
> > > something  
> > > which is an infinite loop is, by definition NOT an infinite loop!  
> > > \<G\>
> > 
> > Not sure Georg Cantor would agree with you. One infinite loop might only go  
> > through a countable (but infinite) number of iterations, i.e. aleph null,  
> > whereas another might go through an uncountable number of iterations.
> 
> --  
> There are some people who begin the Zoo at the beginning, called  
> WAYIN, and walk as quickly as they can past every cage until they get  
> to the one called WAYOUT, but the nicest people go straight to the  
> animal they love the most, and stay there. ~ A.A. Milne (from  
> Winnie-the-Pooh)

---

<div class="post-metadata">

**Author:** ![Louis-Philippe](https://avatars.discourse-cdn.com/v4/letter/l/db5fbb/32.png) [@Louis-Philippe](https://rubytalk.org/u/Louis-Philippe)\
**Post date:** [23 February 2009 17:02 UTC](https://rubytalk.org/t/while-vs-loop/51867/9 "2009-02-23T17:02:59Z")

</div>

This is indeed very fun and mind boggling!Still, pragmatically, not all  
infinite loops are equal, in the performance of their implementations that  
is.  
So taking Ryan's idea to twist iterators to perform infinite loops, here is  
the benchmark again.

For the comparison to be as close as possible between the control  
structures, I use the same inner routine for all, ignoring the fact that I  
could derive incrementation from the infinite range iteration, because this  
is not the subject of the benchmark.

Ruby Infinite loops Inplementations:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
while1 31.960000 0.150000 32.110000 ( 36.749597)  
loop 42.240000 0.190000 42.430000 ( 45.533708)  
Infinite.each 108.400000 0.970000 109.370000 (117.421059)  
for Infinite 112.070000 1.010000 113.080000 (126.712828)

> **···**
>
> #############
> 
> Benchmark.bm do |x|  
> x.report('while1') do  
> n = 0  
> while 1 do  
> break if n \>= 1000000  
> n += 1  
> end  
> end  
> x.report('loop') do  
> n = 0  
> loop do  
> break if n \>= 1000000  
> n += 1  
> end  
> end  
> x.report('Infinite.each') do  
> n = 0  
> (0..(1/0.0)).each do  
> break if n \>= 1000000  
> n += 1  
> end  
> end  
> x.report('for Infinite') do  
> n = 0  
> for i in (0..(1/0.0)) do  
> break if n \>= 1000000  
> n += 1  
> end  
> end  
> end
> 
> #############
> 
> 2009/2/21 Robert Dober \<robert.dober@gmail.com\>
> 
> > On Sat, Feb 21, 2009 at 4:49 PM, Jeff Schwab \<jeff@schwabcenter.com\> \> wrote:  
> > \> Rick DeNatale wrote:  
> > \>  
> > \>\> Anything purporting to be an infinite loop which runs faster than  
> > \>\> something  
> > \>\> which is an infinite loop is, by definition NOT an infinite loop!  
> > \>\> \<G\>  
> > \>  
> > \> Not sure Georg Cantor would agree with you. One infinite loop might only  
> > go  
> > \> through a countable (but infinite) number of iterations, i.e. aleph null,  
> > \> whereas another might go through an uncountable number of iterations.  
> > Might it? I do not think so! I believe that a Turing complete language  
> > can only loop countable times, which of course should be fixed ;).  
> > R.
> > 
> > --  
> > There are some people who begin the Zoo at the beginning, called  
> > WAYIN, and walk as quickly as they can past every cage until they get  
> > to the one called WAYOUT, but the nicest people go straight to the  
> > animal they love the most, and stay there. ~ A.A. Milne (from  
> > Winnie-the-Pooh)
