Louis-Philippe
2/23/2009 5:03:00 PM
[Note: parts of this message were removed to make it a legal post.]
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:
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)
>
>