Solved

Code optimization: looping

Posted on 2004-10-14
7
334 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Does Powershell have you tied up in knots?

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

Suggested Solutions

Title # Comments Views Activity
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 1,758
UPD maximums on Red Hat 6 115
C Language combined operators 28 109
change colour of repeater control in asp.net c# 7 90
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

803 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