From: Aaron Gifford Date: 2010-02-06T03:36:06+09:00 Subject: Fastest way to iterate over a range in steps What's the fastest way to iterate over a range in variable increments? For example, using irb (1.9.1): irb(main):001:0> a=0;s=Time.now;i=0;while(i<1000000000);a+=i;i+=5;end;e=Time.now;p e-s;p a 8.715433734 99999999500000000 => 99999999500000000 irb(main):002:0> a=0;s=Time.now;Range.new(0,1000000000,true).step(5){|i|a+=i};e=Time.now;p e-s;p a 18.932204045 99999999500000000 => 99999999500000000 As you can see I'm stepping by 5 over the range 0 to 1000000000 exclusive (excluding the last item) doing a simple operation each iteration (the operation above is totally contrived). In this case, it appears that while outperforms Range's step method. Wondering, Aaron out.