Hey guys, I'm trying to optimize my code both in RAM and Speed, I'm using teensy 3.2s and while they got enough RAM and speed to handle what i'm doing and then some, i'd still like to optimize things as much as possible.
I have a uint32_t array with a size of 70 elements allocated, additionally a uint8_t to check the current length of that array since it can have 70 or 0 items or anything in between.
Here's a basic example of what i'm talking about
So i'm thinking about changing that to this
So basically i'd be doing an XOR and storing that checksum, i would still save the previous length of the array which would save time by checking the length first, then i wouldn't need an array of 70 items to store the previous items instead i just need 1 uint32_t to store the checksum of the previous items.
So my questions are:
#1 is there a better way to check if an array has changed without storing the previous elements of the array into another array?
#2 can i rely on XOR for this? every uint32_t in the array should be different as it combines five 7-bit bytes into a 32 bit int (with 3 bits ignored) and one of those bytes is always different
#3 would this be slower than comparing the 2 arrays?
I hope this question makes sense, thank you in advance for any thoughts!
I have a uint32_t array with a size of 70 elements allocated, additionally a uint8_t to check the current length of that array since it can have 70 or 0 items or anything in between.
Here's a basic example of what i'm talking about
Code:
uint8_t itemsLength = 0;
uint8_t prevItemsLength = 0;
uint32_t items[70];
uint32_t prevItems[70];
bool checkForChanges(){
if ( itemsLength != prevItemsLength ){
assignNewItemsAsPrev();
return true;
}
for ( uint8_t i = 0; i < itemsLength; i++ ){
if( items[i] != prevItems[i] ){
assignNewItemsAsPrev();
return true;
}
}
assignNewItemsAsPrev();
return false;
}
void assignNewItemsAsPrev(){
prevItemsLength = itemsLength;
for ( uint8_t i = 0; i < itemsLength; i++ ){
prevItems[i] = items[i];
}
}
So i'm thinking about changing that to this
Code:
uint8_t itemsLength = 0;
uint8_t prevItemsLength = 0;
uint32_t items[70];
uint32_t prevItems;
bool checkForChanges(){
if(itemsLength != prevItemsLength){
assignNewItemsAsPrev();
return true;
}
uint32_t checksum = 0;
for(uint8_t i = 0; i < itemsLength; i++){
checksum ^= items[i];
}
if( checksum != prevItems ){
prevItems = checksum;
prevItemsLength = itemsLength;
return true;
}
assignNewItemsAsPrev();
return false;
}
void assignNewItemsAsPrev(){
prevItemsLength = itemsLength;
prevItems = 0;
for(uint8_t i=0;i<itemsLength;i++){
prevItems ^= items[i];
}
}
So basically i'd be doing an XOR and storing that checksum, i would still save the previous length of the array which would save time by checking the length first, then i wouldn't need an array of 70 items to store the previous items instead i just need 1 uint32_t to store the checksum of the previous items.
So my questions are:
#1 is there a better way to check if an array has changed without storing the previous elements of the array into another array?
#2 can i rely on XOR for this? every uint32_t in the array should be different as it combines five 7-bit bytes into a 32 bit int (with 3 bits ignored) and one of those bytes is always different
#3 would this be slower than comparing the 2 arrays?
I hope this question makes sense, thank you in advance for any thoughts!