Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Code optimization: looping

Posted on 2004-10-14
7
Medium Priority
?
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 75 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 135 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 135 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 165 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 conditional statements in the C programming language.
Suggested Courses

618 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