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.
To compare the speed of using a pointer to a normal array index I also rewrote the same program the "normal" way:
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.
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);
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.