--001636c5bd5fda10250493c8d820
Content-Type: text/plain; charset=UTF-8

On Fri, Oct 29, 2010 at 11:59 PM, Ammar Ali <ammarabuali / gmail.com> wrote:

>
> The numbers I got on my machine are:
>
> for loop  1.210000   0.000000   1.210000 (  1.225631)
> while loop  1.460000   0.010000   1.470000 (  1.469653)
> times loop  1.980000   0.000000   1.980000 (  1.998238)
>
> I expected the times version to be faster too, but it looks like the for
> loop is the fastest in this case.
>
>
I spoke too soon, my bad. Once I made the loops do the same work inside, the
numbers got closer to expectations.

require 'benchmark'
include Benchmark

Benchmark.benchmark do |bm|
  bm.report("for loop") {
    for i in 1..1_000_000
      i * i
    end
  }

  bm.report("while loop") {
    i  
    while i < 1_000_000
      i * i
      i + 
    end
  }

  bm.report("times loop") {
    1_000_000.times do |i|
       i * i
    end
  }
end

for loop  1.210000   0.000000   1.210000 (  1.217159)
while loop  1.450000   0.000000   1.450000 (  1.459296)
times loop  1.260000   0.010000   1.270000 (  1.259652)

That's less surprising. The times loop is faster than before.

Regards,
Ammar

--001636c5bd5fda10250493c8d820--