Avatar of naseeam
naseeam
Flag 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!
C

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
Neil Russell

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

ASKER
Will following cause a delay?

for ( ;  counter != 0; counter-- )
{
       x++;
}
ASKER CERTIFIED SOLUTION
evilrix

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
evilrix

>> 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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
naseeam

ASKER
if x is volatile, will above code consume some clock cycles?
evilrix

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.