Solved

Code optimization: looping

Posted on 2004-10-14
7
333 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now