cancel
Showing results for 
Search instead for 
Did you mean: 

Which is best optimized for speed, using 4 separate variables or a 4 dimension Array?

LMorr.3
Senior II

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]

2 REPLIES 2
Muhammed Güler
Senior III

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.

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..