Solved

Code optimization: looping

Posted on 2004-10-14
7
335 Views
Last Modified: 2010-04-15
Dear all,

I have a very simple question.

Code 1:
for (int i=0; i<limits[k]; i++){
}

Code 2:
int limit=limits[k];
for (int i=0; i<limit; i++){
}

Will code 2 runs faster than code 1? if limits[k] will not change the value during the loop, will the compiler smart enough to compare i with a constant value? Or does the compile explicitly loads the address of limits and then go to the k-th element very round of the loop?

Thank you.

With Best Regards
HCK
0
Comment
Question by:hengck23
7 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12307002
depends on the compiler... I'd like to think a cleaver good compiler would produce the same code for both.... (a _really_ good compiler should reduce the code nothing, as the loop is empty, and nothing outside the scope of the loop is getting modified....)
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 25 total points
ID: 12307056
Hi hengck23,
Depends what is in the loop body. The compiler can only optimize code1 when it is absolutely sure that limits[k] and k will not change during the loop.

That's a tricky guess: limits or k could be global and a function modifying them could be called in the loop body. You could provide either's address to a called function - and a zillion of other things.

If you want to be sure that limits[k] is only evaluated once and you don't want to use code 2, count backwards:

for (int i=limits[k]-1; i>=0; i--){
}


Cheers!

Stefan
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 45 total points
ID: 12307522
even though the guards are outside the scope of the loop (global, or even updatable through interrupt?), there is no body to the loop, and there is nothing changed in the loop apart from variables local to the loop body (i.e. "i") - surely a compiler should be safe in reducing this to null code?

However, obviously there will be something in the loop body, so not really an issue I guess....
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

Author Comment

by:hengck23
ID: 12307686
of course, there is something in the loop. It should have been:


Code 1:
for (int i=0; i<limits[k]; i++){
... //does not affect limits[k]
}

Code 2:
int limit=limits[k];
for (int i=0; i<limit; i++){
... //does not affect limits[k]
}
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 45 total points
ID: 12307758
then I'd say there is lilkely to be very little difference (I'd guess that a good compiler would figure out that limit[k] is non-mutating, and as such store it once... unless "limits" is defined as volatile, which would be used to force the compiler to keep the defererence into limits[k] each time.... That said, I'm not entirely sure, and its been years since I've looked at decompiled assembly (which was for 8051 processor!)
0
 

Author Comment

by:hengck23
ID: 12307791
It work be very little difference if the code is run a few times. But for my case, it would be:

for (intk=0; k<bigNumber; k++){
     for (int i=0; i<limits[k]; i++){
          ... //does not affect limits[k]
    }
}

Hence in this case, I was wondering if code 2 would help.
Thanks for the comments!
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 55 total points
ID: 12307795
>>Will code 2 runs faster than code 1?
The only answer here is 'probably, but it might not and it might even run slower'.

'probably' because it is likely but the difference may not be measurable.

'slower' because stack memory might be cached in a different way to 'limits[k]'.

Paul
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question