NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Question on tempsensor - division operation x100?
October 18, 2009 by puargs ![]() |
In the tempsensor example, you have a for loop which executes 100 times, and at the end of each loop performs a division:
In this code, a division operation is being performed 100 times. My question is this - assuming the double temp_avg variable is large enough in memory to hold 100 samples before division, would it be better to place the division outside the for loop? For example:
The reason I ask is because I know division operations take significantly more processing time in cycles. However, what I don't know is if the compiler will catch this and automatically change it so it doesn't matter in the assembly, or if it doesn't make any difference because the microcontroller is RISC based... Does anyone know? When I start diving into making my own projects, I want to be sure on what will/won't affect the number of cycles. Thank you very much in advance! |
---|---|
October 18, 2009 by mrobbins (NerdKits Staff) ![]() |
Hi puargs, You're way should work, and should do the division only once. Indeed, since this is a floating point division, it does take lots of cycles. So yes, go for it! A few quick notes, though:
Hope that helps, Mike |
October 19, 2009 by puargs ![]() |
Exactly the answer I needed, thanks very much! :D |
Please log in to post a reply.
Did you know that our customers love us? Learn more...
|