Solved

Code optimization: looping

Posted on 2004-10-14
7
332 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 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 create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 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

19 Experts available now in Live!

Get 1:1 Help Now