On Thu, Feb 2, 2012 at 23:03, botp <botpena / gmail.com> wrote:

>> (3..8).each do |x|
>> t=Time.now();for a in 0..10**x;end; puts("#{x} #{Time.now()-t}")
>> end
>
> 3 0.000142406
> 4 0.001344933
> 5 0.014539207
> 6 0.076141941
> 7 0.737979205
> 8 7.359555691

Ummmm... yeah... every time you tell it to do ten times as many loops,
it takes almost ten times as long.  What's so surprising?

If you tweak the code to make it say how long each loop took, you'll
see that it actually gets FASTER at first, presumably due to assorted
constant overhead, then a tiny pinch slower (possibly due to the
switch to a different kind of number) but little enough that IMHO
that's lost in the noise.

puts("Pwr  Tot Secs  uS/Loop ")
(3..8).each do |x|
  limit = 10**x
  start_time = Time.now()
  for a in 0 .. limit
    # do nothing here, just timing how long the loops take
  end
  elapsed = Time.now() - start_time
  puts("  #{x}  #{elapsed}  #{elapsed * 1000000.0 / limit} ")
end

will output, with a tiny bit of post-formatting:

Pwr  Tot Secs   uS/Loop
  3   0.000292  0.292
  4   0.001869  0.1869
  5   0.014103  0.14103
  6   0.1058    0.1058
  7   1.051723  0.1051723
  8  10.584073  0.10584073

Of course, as someone mentioned later, the runtime may matter; this is
MRI 1.9.3.

-Dave

-- 
Dave Aronson: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com