Good day,
I wrote this simple code, ATMEGA8A microcontoller
void setup() {
pinMode(1, OUTPUT);
for (byte i = 0; i < 2; i++) {
digitalWrite(1, HIGH);
delay(1000);
digitalWrite(1, LOW);
delay(1000);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
which is executing the loop 2 times,
when compiled it used 888 bytes of storage space.
but when I edited the loop line to 3 times:
for (byte i = 0; i < 3; i++) {
and when compiled it used 868 bytes of storage space.
it used less space, why smaller loop using larger space ?
I think they must use same space or larger space for larger loop.
I'm asking that because I have a large project that occupies all microcontroller space almost, and when I decrease the loop count the program get bigger and doesn't fit the microcontroller.
There are lots of other things you can do to save space.
This program, compiled for the ATmega328P using IDE 1.8.19, takes 944 bytes.
void setup() {
pinMode(1, OUTPUT);
for (byte i = 0; i < 2; i++) {
digitalWrite(1, HIGH);
delay(1000);
digitalWrite(1, LOW);
delay(1000);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
This one, using direct port access to do the exact same things, take 654 bytes.
void setup() {
// pinMode(1, OUTPUT);
DDRD = 2; //PORTD, bit 1 = OUTPUT
for (byte i = 0; i < 2; i++) {
// digitalWrite(1, HIGH);
PORTD |= 2; //set PORTD, bit 1
delay(1000);
// digitalWrite(1, LOW);
PORTD &= ~2; //clear
delay(1000);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
thank you I know that, I use all tricks to get lower space
I put simple code to not go in complex issues.
I'm asking a specific question, why smaller loop gets more space ?
what is going behind the scene
when the loop is bigger than 3 times the program size doesn't affected, only when it's 2 times it gets bigger
that what I'm asking about, I think that's illogical.
Off the top of my head, the compiler is "unrolling" your small loop into two duplicate sections of code, but thinks that the -Os (optimize for size) directive that arduino uses should prevent it from duplicating the code three times.
Compiler Explorer offers only Arduino 1.8.9, and with the -Os option, gives the same length code for both, no unrolling. Only the loop endpoint changes.
You need to look at the actual code generated by the IDE version you are currently using.
Same with int, long, unsigned long... increasing the max value to the size of the type uses the same bytes as 3. Only stating what has been said, that two iterations has an inefficient algorithm in the compiler, where any other number is more efficiently handled.
oooh. An extra 10 instructions, or 2.5% bigger code, and it's somewhat quicker (not that it matters with code full of delay() calls.)
I've often noticed that gcc's idea of "size optimization" doesn't seem to be strongly based on actual object code - there are some things that seem to "blow up" at the instruction level that gcc seems to think are "small." (multi-bit shifts, for one, IIRC.)
Note that in the looping example, the compiler has determined that the code is looping 3 times, instead of literally incrementing a variable and testing against 3...
For this particular for loop, yes. If the body of the loop were longer, perhaps not.
I believe that there are individual optimization switch that can turn off Just loop unrolling, and using gcc-specific pragmas you can turn them on and off for small segments of,code
For this particular for loop, yes. If the body of the loop were longer, perhaps not.
I believe that there are switches to control individual optimization settings, that can be included on a per-function basis in the source code via a gcc "pragma"... (Hmm. With various experiments, I couldn't get it to NOT unroll the 2x loop.)
// Doesn't work :-(
#pragma GCC push_options
#pragma GCC optimize("no-unroll-loops", "no-peel-loops")
void setup() {
pinMode(1, OUTPUT);
for (byte i = 0; i < 2; i++) {
digitalWrite(1, HIGH);
delay(1000);
digitalWrite(1, LOW);
delay(1000);
}
}
#pragma GCC pop_options
we live in poor country, we must create the cheapest product to can sell, so it's important to use this microcontroller, I believe that it works, but I'm looking for solutions.