PDA

View Full Version : Loopy thoughts



Lima Bean
10-16-2011, 01:54 PM
Recursive coding is pretty, but it can hide small things that can and will lead to
"features" and uninteneded consequences as a code base grows.

In a time gone by loops could have been taken for granted, because the hardware was sequential and much slower, but with the advancement of speed and capacity this has changed.

With each new processor there are also new machine instructions, that means even the simplest of primitives, like int and str, are shuttled around the cache registers and from processor to processor differently as well. The probability of "hiccups" will also increase with the more cores on the die. In addition to this, more coding is coming alongthat handles all Oses and file systems.

A caveat must be mentioned here, loops are executed from the point of the computer, not the coder. This is very easy to forget as confidence and skill increase. Why mention the obvious?

repeat
....
until Boolean

while Boolean do
.....

Speed is what the repeat loop is good for, but how many lines are parsed and executed before the value of until is returned or met? If it is parsed from the top down, then this will cause an unintended error. On the flip-side what if it is parsed from the bottom up? In that case the while loop will be passing a bad Boolean.

Yes, stuff does happen, and loops can be solid, but how many lines are executed and which way are they parsed? Once that is established then "features" can be planned, not unexpected pluses.


Both of these links provide code and comparison of more advanced looping techniques

http://blogs.oracle.com/greimer/entry/best_way_to_code_a

http://www.codeproject.com/KB/cs/FastLessCSharpIteration.aspx

Echo_
10-20-2011, 07:39 PM
Recursion can be very powerful when used correctly, just look at any good common lisp program. More languages should feature something like CL's trace function tbh

Bixby Sayz
10-20-2011, 10:24 PM
Speed is what the repeat loop is good for, but how many lines are parsed and executed before the value of until is returned or met? If it is parsed from the top down, then this will cause an unintended error.
Wah? I seem to missed the point of this statement. A repeat loop is supposed to be parsed from the top down. That's the whole point of that loop: Execute these statements, test a condition, and based on the test possibly do it again.

If I need a loop that executes at least once before testing the condition I use repeat taking care to set the boolean somewhere in the loop. If I need to execute if (and only if) the condition I use a while or if, taking care to make sure the boolean is set before testing.

Given that, I'm reading your statement and left scratching my head. If the compiler does "bad things" with properly formed loops, why are you using it in the first place?

Lima Bean
10-21-2011, 02:12 AM
@Bixby Says The last scripting engine I used parsed lines of script 10 lines per cycle, and 50 ms delay default.

Repeat until is faster than the while do loops, in that engine, which caused unexpected results.

I am still not clear on some of the basic stuff Simba can and can't do, but that is being cleared up the more I use it.

i luffs yeww
10-21-2011, 04:25 AM
What scripting engine were you using? :x

And Simba (PS) works as most interpreted languages do/should, imo. There aren't too many hiccups, as long as you know it's interpreted.

Lima Bean
10-21-2011, 04:59 AM
The scripting engine wasn't for RS, it was for Ultima Online, the engine is Easyuo at site of the same name.