For Loop Best Practices
jabramson
Posts: 106
I have several for loops that I use in my program. I've been assigning each one a different variable to use as the counter but I got to thinking, do I need to do that?....Would a for loop ever run while another for loop is running (with the exception of a loop in a loop). How about if I have a wait command that then runs a for loop? As far as I understand netlinx, there's never actually 2 things running at once, one thing ends then the next in the queue goes.
0
Comments
That being said, it is good practice however to limit the scope of any variables as much as possible. In the case of your loops if you are just utilizing a variable as a counter this can be achieved with a stack_var:
Ex:
Having worked with several programming languages and embedded devices, I don’t think that’s accurate. Running a loop backwards will only make the program harder to understand if the loop makes more sense when run forward. Just because the condition in a for loop doesn’t have a comparison operator doesn’t mean that fewer calculations are performed. In the code above, the implicit comparison acts like nloop != 0. If you look at the PowerPC instruction set, which I believe is what the NI-X100 masters use, the for loop’s condition statement is going to get compiled down to the cmp instruction. This instruction compares the values in two CPU registers no matter how the code you write may look. If you don’t give the compiler a value to compare, it figures one out when it builds the machine instruction.
Basically, write code in a way that makes sense when read and let the compiler do the machine-level optimizations.
Also relevant to this thread is the tech note on why loops in mainline are bad.
See [thread=5294]this thread[/thread].
As you mentioned, in the majority of cases making something readable trumps optimizing the crap out of it. That being said though the two are not mutually exclusive.
Oh, I write normal when necessary. But I've seen backwards loops in all the languages I've programmed. It's pretty commonly used for exactly the same reasons mentioned in the thread referenced in Phreak's reply. I first ran into it when modifying some C++ code back in 1999. The science behind it is pretty easy to understand and makes total sense. I only use it when the direction of something doesnt matter. For example if you're populating a bunch of text boxes on a TP what difference does the order make? As for the importance or lack thereof of code optimization: I've written code for some big systems that sped up noticably after doing some pretty hardcore optimizing.
Take this for example:
You can use brackets anywhere to create an extra scope to define stack_vars if you'd like.
I'd like to point out in DebugModuleExample from TechNote 875, tokenizer.axi includes this in the file revision comments:
It looks like I was wrong about how NetLinx handles “i > 0” versus plain old “i”. However, it seems that the only loop that causes a drastic difference is when a function is in the evaluation section, like “for (i = 1; i < length_array(data); i++)”. That makes sense because the function will be called during each pass of the loop. Evaluating primitives doesn’t seem to make a big difference as to how the loop is written.
If you were able to get a noticeable performance boost then yeah, it sounds like the optimizations completely make sense. I think I’ve been tainted, having spent the last nine months on and off re-architecting another programmer’s various “optimizations” across several different projects that had no visible performance benefits, made future expansion difficult, and had little or no comments.