[Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did an [analysis][2] involving the following variations:
- Basic
void loop()
(which gets inlined on compilation) - Un-inlined
void loop()
(using__attribute__ ((noinline))
) - Loop with
while(1)
(which gets optimized) - Loop with un-optimized
while(1)
(by adding__asm__ __volatile__("");
. This is anop
instruction that prevents optimization of the loop without resulting in additional overheads of avolatile
variable) - An un-inlined
void loop()
with optimizedwhile(1)
- An un-inlined
void loop()
with un-optimizedwhile(1)
The sketches can be found [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which [bad things happen][5]).
##Results I then calculated the mean execution times of each loop, subtracted 100 milliseconds from each and then plotted the results.
[![][6]][7]
#Conclusion
- An un-optimised
while(1)
loop withinvoid loop
is faster than a compiler optimisedvoid loop
. - The time difference between the un-optimized code and default Arduino optimized code is insignificant practically. You will be better off compiling manually using
avr-gcc
and using your own optimisation flags rather than depending on the Arduino IDE to help you with it (if you need microsecond optimisations).
**NOTE:** The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time includes a call to `Serial.println`, `micros` and `delay`. **NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: httphttps://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339 [2]: http://github.com/AsheeshR/Arduino-Loop-Analysis [3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches [4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data [5]: https://electronics.stackexchange.com/q/12300/18583 [6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png [7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csv
[Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did an [analysis][2] involving the following variations:
- Basic
void loop()
(which gets inlined on compilation) - Un-inlined
void loop()
(using__attribute__ ((noinline))
) - Loop with
while(1)
(which gets optimized) - Loop with un-optimized
while(1)
(by adding__asm__ __volatile__("");
. This is anop
instruction that prevents optimization of the loop without resulting in additional overheads of avolatile
variable) - An un-inlined
void loop()
with optimizedwhile(1)
- An un-inlined
void loop()
with un-optimizedwhile(1)
The sketches can be found [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which [bad things happen][5]).
##Results I then calculated the mean execution times of each loop, subtracted 100 milliseconds from each and then plotted the results.
[![][6]][7]
#Conclusion
- An un-optimised
while(1)
loop withinvoid loop
is faster than a compiler optimisedvoid loop
. - The time difference between the un-optimized code and default Arduino optimized code is insignificant practically. You will be better off compiling manually using
avr-gcc
and using your own optimisation flags rather than depending on the Arduino IDE to help you with it (if you need microsecond optimisations).
**NOTE:** The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time includes a call to `Serial.println`, `micros` and `delay`. **NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: http://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339 [2]: http://github.com/AsheeshR/Arduino-Loop-Analysis [3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches [4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data [5]: https://electronics.stackexchange.com/q/12300/18583 [6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png [7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csv
[Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did an [analysis][2] involving the following variations:
- Basic
void loop()
(which gets inlined on compilation) - Un-inlined
void loop()
(using__attribute__ ((noinline))
) - Loop with
while(1)
(which gets optimized) - Loop with un-optimized
while(1)
(by adding__asm__ __volatile__("");
. This is anop
instruction that prevents optimization of the loop without resulting in additional overheads of avolatile
variable) - An un-inlined
void loop()
with optimizedwhile(1)
- An un-inlined
void loop()
with un-optimizedwhile(1)
The sketches can be found [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which [bad things happen][5]).
##Results I then calculated the mean execution times of each loop, subtracted 100 milliseconds from each and then plotted the results.
[![][6]][7]
#Conclusion
- An un-optimised
while(1)
loop withinvoid loop
is faster than a compiler optimisedvoid loop
. - The time difference between the un-optimized code and default Arduino optimized code is insignificant practically. You will be better off compiling manually using
avr-gcc
and using your own optimisation flags rather than depending on the Arduino IDE to help you with it (if you need microsecond optimisations).
**NOTE:** The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time includes a call to `Serial.println`, `micros` and `delay`. **NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: https://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339 [2]: http://github.com/AsheeshR/Arduino-Loop-Analysis [3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches [4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data [5]: https://electronics.stackexchange.com/q/12300/18583 [6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png [7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csv
[Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did an [analysis][2] involving the following variations:
- Basic
void loop()
(which gets inlined on compilation) - Un-inlined
void loop()
(using__attribute__ ((noinline))
) - Loop with
while(1)
(which gets optimized) - Loop with un-optimized
while(1)
(by adding__asm__ __volatile__("");
. This is anop
instruction that prevents optimization of the loop without resulting in additional overheads of avolatile
variable) - An un-inlined
void loop()
with optimizedwhile(1)
- An un-inlined
void loop()
with un-optimizedwhile(1)
The sketches can be found [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which [bad things happen][5]).
##Results I then calculated the mean execution times of each loop, subtracted 100 milliseconds from each and then plotted the results.
[![][6]][7]
#Conclusion
- An un-optimised
while(1)
loop withinvoid loop
is faster than a compiler optimisedvoid loop
. - The time difference between the un-optimized code and default Arduino optimized code is insignificant practically. You will be better off compiling manually using
avr-gcc
and using your own optimisation flags rather than depending on the Arduino IDE to help you with it (if you need microsecond optimisations).
**NOTE:** The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time includes a call to `Serial.println`, `micros` and `delay`. **NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: http://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339 [2]: http://github.com/AsheeshR/Arduino-Loop-Analysis [3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches [4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data [5]: httphttps://electronics.stackexchange.com/q/12300/18583 [6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png [7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csv
[Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did an [analysis][2] involving the following variations:
- Basic
void loop()
(which gets inlined on compilation) - Un-inlined
void loop()
(using__attribute__ ((noinline))
) - Loop with
while(1)
(which gets optimized) - Loop with un-optimized
while(1)
(by adding__asm__ __volatile__("");
. This is anop
instruction that prevents optimization of the loop without resulting in additional overheads of avolatile
variable) - An un-inlined
void loop()
with optimizedwhile(1)
- An un-inlined
void loop()
with un-optimizedwhile(1)
The sketches can be found [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which [bad things happen][5]).
##Results I then calculated the mean execution times of each loop, subtracted 100 milliseconds from each and then plotted the results.
[![][6]][7]
#Conclusion
- An un-optimised
while(1)
loop withinvoid loop
is faster than a compiler optimisedvoid loop
. - The time difference between the un-optimized code and default Arduino optimized code is insignificant practically. You will be better off compiling manually using
avr-gcc
and using your own optimisation flags rather than depending on the Arduino IDE to help you with it (if you need microsecond optimisations).
**NOTE:** The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time includes a call to `Serial.println`, `micros` and `delay`. **NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: http://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339 [2]: http://github.com/AsheeshR/Arduino-Loop-Analysis [3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches [4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data [5]: http://electronics.stackexchange.com/q/12300/18583 [6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png [7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csv
[Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did an [analysis][2] involving the following variations:
- Basic
void loop()
(which gets inlined on compilation) - Un-inlined
void loop()
(using__attribute__ ((noinline))
) - Loop with
while(1)
(which gets optimized) - Loop with un-optimized
while(1)
(by adding__asm__ __volatile__("");
. This is anop
instruction that prevents optimization of the loop without resulting in additional overheads of avolatile
variable) - An un-inlined
void loop()
with optimizedwhile(1)
- An un-inlined
void loop()
with un-optimizedwhile(1)
The sketches can be found [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which [bad things happen][5]).
##Results I then calculated the mean execution times of each loop, subtracted 100 milliseconds from each and then plotted the results.
[![][6]][7]
#Conclusion
- An un-optimised
while(1)
loop withinvoid loop
is faster than a compiler optimisedvoid loop
. - The time difference between the un-optimized code and default Arduino optimized code is insignificant practically. You will be better off compiling manually using
avr-gcc
and using your own optimisation flags rather than depending on the Arduino IDE to help you with it (if you need microsecond optimisations).
**NOTE:** The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time includes a call to `Serial.println`, `micros` and `delay`. **NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: http://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339 [2]: http://github.com/AsheeshR/Arduino-Loop-Analysis [3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches [4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data [5]: https://electronics.stackexchange.com/q/12300/18583 [6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png [7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csv
Cybergibbons's answer [Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did a small analysis an [analysis][2] involving the following variations:
The sketches can be found here [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating 300 data points each [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which bad things happen [bad things happen][5]).
http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png [![][6]][7]
NOTE: The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time include a call to Serial.println
, micros
and delay
.
NOTE2: This was done using the Arduino IDE and the default compiler flags that it supplies.
**NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: http://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339[2]: http://github.com/AsheeshR/Arduino-Loop-Analysis[3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches[4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data[5]: http://electronics.stackexchange.com/q/12300/18583[6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png[7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csvCybergibbons's answer describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did a small analysis involving the following variations:
The sketches can be found here.
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating 300 data points each. There was a 100 millisecond delay
call in each loop (without which bad things happen).
http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png
NOTE: The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time include a call to Serial.println
, micros
and delay
.
NOTE2: This was done using the Arduino IDE and the default compiler flags that it supplies.
[Cybergibbons's answer][1] describes quite nicely the assembly code generation and the differences amongst the two techniques. This is intended to be a complementary answer looking at the issue in terms of practical differences, i.e. how much of a difference either approach will make in terms of execution time.
##Code Variations I did an [analysis][2] involving the following variations:
The sketches can be found [here][3].
##Experiment
I ran each of these sketches for 30 seconds, thereby accumulating [300 data points each][4]. There was a 100 millisecond delay
call in each loop (without which [bad things happen][5]).
[![][6]][7]
**NOTE:** The actual time values are not of significance here, the difference between them is. The ~90 microseconds of execution time includes a call to `Serial.println`, `micros` and `delay`. **NOTE2:** This was done using the Arduino IDE and the default compiler flags that it supplies. **NOTE3:** Analysis (plot and calculations) was done using R. [1]: http://arduino.stackexchange.com/questions/337/would-an-infinite-loop-inside-loop-perform-faster#339[2]: http://github.com/AsheeshR/Arduino-Loop-Analysis[3]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Code/Sketches[4]: http://github.com/AsheeshR/Arduino-Loop-Analysis/tree/master/Data[5]: http://electronics.stackexchange.com/q/12300/18583[6]: http://raw2.github.com/AsheeshR/Arduino-Loop-Analysis/master/Figures/timeplot.png[7]: http://github.com/AsheeshR/Arduino-Loop-Analysis/blob/master/Data/Munged/runtime.csv