From: Dave Howell Date: 2006-06-02T05:55:02+09:00 Subject: Re: How use Profiling, a Quick Guide. On Jun 1, 2006, at 13:47, Victor Shepelev wrote: > From: Dave Howell [mailto:groups@grandfenwick.net] > Sent: Thursday, June 01, 2006 11:36 PM >>>>> * Large number of calls to .size and .length and .count_objects >>>>> methods are a >>>>> clue some nidjit somewhere is doing something really N^2 stupid >>>>> like... >>>>> for( i=0; i < container.count_objects(); i++) { >>>>> } >>>> >>>> question_stack << "What exactly is stupid about that? And what's the >>>> smart alternative? >>> max=container.count_objects() >>> for( i=0; i < max; i++) { >>> } >>> >>> container.each.... >> >> OK, so "container.each" is presumably the smart alternative, and what >> I'd instinctively use anyway, but I don't understand what it is about >> "for( i=0; i < max; i++)" that makes it "really n^2 stupid." > > The point was: if you use some value repeatedly, it is generally > smarter to > pre-calculate it. {brief scratching of head} Oh! So BOTH of Sam's examples were 'non-stupid' rewrites of the initial code. I completely failed to recognize the 'for' loop as being Ruby code in the first place. Does Ruby really have an ++ operator? I missed that somehow. Damn. As embarrassingly illustrated by the fact that I coded my own ".increment" operator in the initial message instead of using it. my_clues++