Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Does empty for loop cosume clock cycles?

My target is based on Freescale 68HCS12 Microcontroller.  I need to create small dealys in my code.

for ( ;  counter != 0; counter-- )
{
       ;
}


Will this actually consume clock cycles or it'll just get optimized by Cosmic Compiler and I won't get any delay?


Thank you!
Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

No, it will be removed by the optimizer in the compiler and ignored.
Avatar of naseeam

ASKER

Will following cause a delay?

for ( ;  counter != 0; counter-- )
{
       x++;
}
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Will following cause a delay?
Rather than Q&A, it would be far simpler if you tell us what it is you are trying to do.
Avatar of naseeam

ASKER

if x is volatile, will above code consume some clock cycles?
Quite possibly since the compiler probably won't be able to optimise away the use of x; however, it is really down to how the compiler's optimiser works. If it can figure out that ignoring x will cause no side-effects it might still optimise away the loop; however, I wouldn't like to put money either way -- your best be is to test it.

Compiler optimisation is a tricky subject and as programmers we're pretty poor at 2nd guessing them.