From: Joel VanderWerf Date: 2002-09-20T10:46:32+09:00 Subject: Re: How to Efficiently Calculate the Pattern of Zeros and Ones? No doubt the state machine approach is faster, and you're also accurate about shifting. I'm just relieved that each_with_neighbors doesn't spew garbage, which would make it even slower :-) William Djaja Tjokroaminata wrote: > Hi, > > If the "shifts data through it" is accomplished using Ruby > Array#push() and Array#shift(), then probably the code is indeed very easy > to write, but probably not very efficient in the background. I like the > answer given in the first response, as I think it mimics how regular > expression works (using some "states"). > > Regards, > > Bill > =========================================================================== > Joel VanderWerf wrote: > >>I had to go back and check whether each_with_neighbors generated lots of >>arrays or not. It turns out that it doesn't. Instead, it keeps a single >>array, shifts data through it, and yields the array. So it should be >>fairly efficient as pure ruby code goes. > > >>However, this may surprise users who modify the array in the iterator >>block, so I'll slip a warning into the docs. > > >>If your rows are short, each_with_neighbors is not very efficient >>because of the "warm up" and "cool down" to deal with the cases near the >>edge. > -- Joel VanderWerf California PATH, UC Berkeley mailto:vjoel@path.berkeley.edu Ph. (510) 231-9446 http://www.path.berkeley.edu FAX (510) 231-9512