2022-05-06 06:46 AM
I want to ensure I've used the most optimized code. I'm using gcc on linux. I have 4 values to store. If I use an array, I can for() loop as a convenience and shorter code, but is there added overhead compared to just using separate variables and having a few more lines of repetitive code? Will using the for() loop use up more ticks? Sorry if I'm splitting hairs as I am not sure.
So I am wondering if using:
uint8_t varA1;uint8_t varA2;uint8_t varA3;uint8_t varA4;
is faster than using:
uint8_t varA[4]
2022-05-06 08:09 AM
Both take up the same space in ram. Your for loop will probably be processed in a much shorter time than the method of obtaining the data (unless you're copying from ram to ram).
I suggest you choose the more understandable one.
There will be a few cycles difference. I usually avoid copy-pasting 10 or 200 times with a for loop, but a few times I leave it as is.
2022-05-06 09:00 AM
I would tend to avoid global variables, and dozens of similarly named variables.
The code will be far cleaner with arrays, and the optimizer can likely index of the same basis, rather than the ARM implementation loading an Address into a register, and then loading the content pointed to by the address into another register. Architecturally the ARM will work better with the array/pointer model.
If the optimizer can hold values in registers, rather than memory, that will speed things up significantly.
Review the code output by the compiler, and judge how effective it is. Work on algorithms, compilers aren't good at understanding concepts at that level.