Array Pointers just as fast as Normal Arrays?

Kuba0040

Well-known member
Hello,
I am currently writing a program where speed is a big factor for me. I have a 2048 cell array in a separate .h file that I want to be able to read quickly.
I want to use a pointer to do that, here is how I’ve set it up, the contents of myArray as well as the variable read_value are both int16_t. array_index is a uint16_t variable.
Code:
int16_t *array_pointer=myArray;
read_value=*(array_pointer+array_index);
To compare the speed of using a pointer to a normal array index I also rewrote the same program the "normal" way:
Code:
read_value=myArray[array_index];

The test consisted of measuring the time taken to execute both the read_value=*(array_pointer+array_index); line of code, as well as in a separate run, read_value=myArray[array_index];

Both programs returned the same value, which was to be expected. However, upon looking at the execution time I noticed that the "normal" program executed just as quickly as the pointer based one. Both finishing in 4 CPU cycles on a Teensy 4.0.

So, my question is, what's the benefit to using pointers on ARM microcontrollers? When they apparently are just as fast as normal arrays.

I know I must be missing something, if anyone can explain why the execution times are the same, I'll be grateful.
Thank you.
 
Depends... :)
..on how the compilers decides to translate it.
If he is out of registers in a more complicated loop, it is of some use not to need an index. Often, it is possible to remove the index (to optimize it away) - which results in the same instructions (pinter vs array), but sometimes not.
Then, the index takes one additional register, which may or may not make it run a bit slower. But there is no general rule. As said, it depends...

Just write it as simple as possible. Then, it's easier for the compiler, to optimize it.

And, then, in C there is no real difference between an array and a pointer.. :)
 
But as general hints for speed:
- Don't make the array const - this way it will be in RAM and does not have to be read from the flash.
- Make it a local variable if possible, so that the stack-pointer(+index) can be used
- make it part of a struct with all vars needed for the algorithm / function. This way the compiler does not need to generate code for address loads and can use offsets instead.
 
On Teensy 4, I thought const variables, while they reside in flash by virtue of being stored in the program, are copied to RAM? It was also my understanding that they don’t get copied to RAM if specified as PROGMEM.

It’s also my understanding that on Teensy 3, const variables don’t get copied to RAM.
 
So, my question is, what's the benefit to using pointers on ARM microcontrollers? When they apparently are just as fast as normal arrays.
In C and C++ pointers and arrays are basically interchangable, different views on the same memory model, so you'd
never expect a difference in performance whatever processor.

You can't pass composite objects in C (C++ is more complex), so arrays are always passed behind the scenes as pointers,
the value is not copied.

See for instance: https://stackoverflow.com/questions/7454990/why-cant-we-pass-arrays-to-function-by-value
 
As said. Depends ... :) Yes they are passed as pointers. There can be a difference, esp. if the loop/array (or pointer) index is used for other things, too. And the compiler can choose to either - for example - to increment the pointer, or an offset.. whatever it thinks is better in the code in question. ARM Cortex has efficient instructions for both. Other CPUs not compulsory.
 
Back
Top