Abinoam Jr. wrote in post #1132463:
I completly agree with Tony Arcieri.
The thing is even less important If the code inside the block is
I/O-bound.
There will be no difference at all.
But there's a more important implication in my humble opinion.
Desing tests for while loops are harder.
You cannot (easily) mock/stub anything around it.
With methods and blocks I can easily make tests to guarantee that an
specific method is yielding an expected object.
Also, I can ensure that the block has some side effect.
Or I can test if the block is returning some kind of value.
So I've been seeing code all over that break the logic more and more
in separate small chunks of codes on methods so that testing could be
easier.
Some people prefer this even if it gives a small performance penalty.
But, this allegations of a performance penalty generally fall to the
ground when real code is benchmarked.
Abinoam Jr.
Well, my point is I think it should be documented, for the benefits of
all users, that WHILE loops can potentially make some code faster. After
all, Matz included them natively in the language, so he assumedly
envisioned a use for them, and I have shown they -can- make code faster.
I'm looking for speed (in Ruby), so a reduction from 7 to 4 seconds (43%
reduction) is significant, especially when I don't have to change the
algorithm but merely need to implement it with different instructions.
Please appreciate that this sort of information is REALLY important to
me. And this kind of information can make Ruby more attractive to people
doing scientific, math, and numerical/algorithmic programming.
As I said, for most cases WHILE loops wouldn't be the first choice and
the other more idiomatic structures SHOULD first be used (as I did),
just to get code to work. However, this implementation tweek should be
stated in the books and documentation, because it provides a tangible
difference that can make WHILE loops the best implementation choice.
···
From my point of view, I do scientific and math related algorithms, and
--
Posted via http://www.ruby-forum.com/\.