In addition to reducing code size, selecting a proper data type and size will reduce execution time as well. For AVR 8-bit, accessing 8-bit (Byte) value is always the most efficient way.
The table below shows the difference between 8-bit and 16-bit variables.
16-bit variable | 8-bit variable | |
---|---|---|
C source code |
#include <avr/io.h> int main(void) { uint16_t local_1 = 10; do { PORTB ^= 0x80; } while (--local_1); } |
#include <avr/io.h> int main(void) { uint8_t local_1 = 10; do { PORTB ^= 0x80; } while (--local_1); } |
AVR Memory Usage |
Program: 94 bytes (1.1% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) |
Program: 92 bytes (1.1% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) |
Cycle counter | 90 | 79 |
Compiler optimization level | -O2 | -O2 |