0

I am working on a project where i need to store as fast I can following :

1 - Acceleration on X

2 - Acceleration on Y

3 - Acceleration on Z

4 - timestamp

5 - A value from some other sensor

Because I need to get these as fast as I can I am thinking to not do any calculation while acquire values and just store them for 30 -60 seconds and then when the "test" is complete to compute calculations. My question is if will be better to create 5 arrays and put these values there or create a matrix and store values there. I need these values to be stored like they are "connected" e.g. on reading 1 i get 5 values and put them on array[0] , then on reading 1 and so on Also an important thing to mention, this will run on a esp32.

Thank you

  • 1
    The time needed to store these values in RAM will most likely be completely dwarfed by the time needed to read the values from the sensors. – Edgar Bonet May 13 at 12:30
  • Thanks a lot, I think you are right – David May 13 at 12:46
3

I fully agree with Edgar Bonet.

My opinion is keeping a 'matrix' is better, or actually one array (of structs), where each array elements stores all information for one measurement (which is stored inside one struct). This has some benefits:

  • Data of one measurement is consecutive in memory, so you can easily transfer it (to some external memory or sending it). It's exact one struct (and you can use sizeof(struct_item) to know how many bytes you have to transfer.

  • It's good practice in general to keep information together, creating 5 always-equal length arrays seems strange.

  • If for some reason you want to make a single array dynamic or change it to a ring buffer for example, you can easily add/remove items (instead of dynamically changing 5 arrays).
  • Locality of change (see comments of Kwasmich/DuncanC)

There is also one benefit about having separate arrays (where each array stores one type of information, like an acceleration or timestamp: when you have a function (especially in another class) that only needs one of them, you can specifically send that array (or pointer/reference), so the other information is hidden from that function.

| improve this answer | |
  • You are right but now rise another question: If i put all in one array then value[x] should contain all - probably i need concatenate them somehow accX,accY...so i will have a string array – David May 13 at 12:49
  • 1
    You know there are religious fights going on abaout AoS vs. SoA (Arrays of Struct versus Structure of Arrays). It is mostly about data locality which does not apply to MCUs because of the lack of caches. – Kwasmich May 13 at 12:49
  • @David the idea is to make an array of structs where you put in the values. – Michel Keijzers May 13 at 12:52
  • @Kwasmich I didn't know those fights are so hard/religious. However, I can mention something about it in my answer (so I'm going to adapt). – Michel Keijzers May 13 at 12:53
  • 1
    For modern CPUs or GPU farms, locality of reference can make a huge improvement in cache coherence, and thus a huge speed improvement. That would suggest an array of structs, where all the values for a single calculation were grouped together. However, as Kwasmich says, that doesn't apply to MCUs like Arduinos. – Duncan C May 13 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.