PDA

View Full Version : Looping methodology


MattyJim
08-08-2010, 05:34 PM
A lot of the coding examples I see being used around the web repeatedly call the .length method when iterating through a fixed length array.

Something like this, for example:

char[] MyArray = new char[4] { 't', 'e', 's', 't' };
for (int i = 0; i < MyArray.Length; i++)
HttpContext.Current.Response.Write("[" + MyArray[i] + "]");



Am I correct in thinking that it's more efficient to establish the array length before entering the loop.......


char[] MyArray = new char[4] { 't', 'e', 's', 't' };
int MyArrayLen = MyArray.Length;
for (int i = 0; i < MyArrayLen; i++)
HttpContext.Current.Response.Write("[" + MyArray[i] + "]");


.......rather than repeatedly calculating the same value, or is the code somehow optimised when compiled?


I realise that the first method keeps the code much shorter (which I guess would be better for certain requirements), but does anyone out there know whether this approach carries additional overhead?

Fou-Lu
08-08-2010, 07:11 PM
I would expect this to be dependent on the language. If the compiler is smart, it may do something to optimize this for you.
Otherwise, yes there is generally an overhead attached with pulling a size from within the loop conditions. I generally use PHP which is interpreted, so each hit on the count($myarray) would result in a new call to the count method. If I use a for loop, I typically write it outside:

$myStr = 'test';
$myStrLength = strlen($myStr);
for ($i = 0; $i < $myStrLength; ++$i)
{
print $myStr[$i] . PHP_EOL;
}

Could be an array as well, but since you had a char array I decided to also stick with a string.

accwebworks
08-11-2010, 06:09 PM
Do not worry about micro-optimization like this. .Net compiler will optimize this for you.
Make sure your code is clean and easy to read.
If you're still concerned, measure time elapsed for each example. I'll bet they take the same time to finish.

Fou-Lu
08-11-2010, 06:13 PM
Do not worry about micro-optimization like this. .Net compiler will optimize this for you.
Make sure your code is clean and easy to read.
If you're still concerned, measure time elapsed for each example. I'll bet they take the same time to finish.

On top of this, another question is do you really care? I mean, if your not writing for embedded devices of any sorts, then I doubt that a little bit of extra cpu cycles or memory would make that much of a difference.
With a loop like this in PHP, after 100,000 iterations I'd expect the difference to be within 1/1000 of a second on modern machines, so is it really worth the effort?
Something like this I usually say: code it this way from here on, but don't bother going back to fix it.

oracleguy
08-11-2010, 07:03 PM
On top of this, another question is do you really care? I mean, if your not writing for embedded devices of any sorts, then I doubt that a little bit of extra cpu cycles or memory would make that much of a difference.
With a loop like this in PHP, after 100,000 iterations I'd expect the difference to be within 1/1000 of a second on modern machines, so is it really worth the effort?
Something like this I usually say: code it this way from here on, but don't bother going back to fix it.

Speaking in more general programming terms, embedded device or not you might still want to care. It depends how often a given block of code is going to be ran. If it might get called thousands of times per second, then those fractions will start to add up.

As for this specific optimization, it depends on the language and on the compiler itself as to if it will optimize the loop conditional or not. And if you have a more expensive function call in the condition, it is better to just be sure and make it a variable. Then the compiler might just optimize your variable to a register.

MattyJim
08-30-2010, 03:44 PM
Thanks to everybody for your replies! :)

I mainly code in C# these days, so it's good to know that I'll be able to keep my code clean and take advantage of the .NET optimisation in most cases.

I've done quite a lot of messing around with Javscript animations (NewsTickers, etc) in the past though, which has involved nesting loops and running code on clients with unknown specs, so I think I'll carry on being extra cautious for those special circumstances.


Thanks again :):):):):):):)

Fou-Lu
08-30-2010, 07:25 PM
I would expect that C# would optimize this for you. Oracleguy is right, technically you should take great care in doing what you need to for optimization, regardless of scenario. I still stand by what I usually say: 'optimize this now, but don't bother going back :p'.

Javascript I would say needs a lot of care. The problem with it is that I have no idea if, or what if anything does, optimize it. And since these are client based, its impossible to judge what kind of memory you have available to you at this point. Who knows, maybe doing a count like this inside a loop (I don't recall if JS actually has this type of function O.o) could push a clients browser to a grinding halt with all the allocations and releases that I'm sure the sizeof() style methods have to deal with.