From: Csaba Henk Date: 2005-02-28T16:55:00+09:00 Subject: Re: for vs. each On 2005-02-27, Robert Klemme wrote: > > "Fear Dubh" schrieb im Newsbeitrag > news:cvslbj$9ns$1@reader01.news.esat.net... >> Does each have a fatter frame then? >> Is "for ... in ... do ..." more efficient? > > I would believe not, but you can test this yourself (hint "ruby -r profile" > and module Benchmark). $ ruby -v ruby 1.8.2 (2004-12-25) [i386-linux] ruby -rbenchmark -e 'n=10**6; [ proc{ (0...n).each{|x| x} }, proc{ for x in 0...n; x ;end }, proc{ n.times{|x| x} } ].each {|pr| puts Benchmark.measure(&pr) }' 0.370000 0.000000 0.370000 ( 0.376541) 0.320000 0.000000 0.320000 ( 0.339880) 0.370000 0.000000 0.370000 ( 0.391311) So for is slightly faster. But see, do you wonder what happens if you execute the above three benchmarks in reverse order? Just replace the "each" (after the three-element array) with "reverse_each"... ;) Csaba