|
|
View previous topic :: View next topic |
Author |
Message |
bwgames
Joined: 21 Jul 2005 Posts: 36
|
Fast way of checking array? |
Posted: Wed Aug 03, 2005 3:05 am |
|
|
Here's my background:
I have 64 arrays each containing a 12bit binary number.
I'm reading in an external input into an array (also a 12bit binary number).
I need a way to compare the arrays.. Speed isn't a major issue, but the faster the better, given the large amount of arrays.
For some reason, I can't get strcmp to work, so I've started to resort to using
(data and Array1 are both integers defined in the form int data[15]; and int Array1[15]={0,0,0,1,1,0,1,1....etc};) - Reason for [15] is that the input has 3 zero's preceding it, hence need to store 15 instead of 12. I am planning on discarding that part, when I have the compare working..
Code: |
for(i=0;i<=14;++i) {
if(data[i]==Array1[i]){
Array1True=Array1True++;
};
if(!data[i]==Array1[i]){
Array1True=0;
};
if(Array1True==15) {
printf("Data matches Array1");
};
Array1True=0;
|
I could slim it down by leaving out if(!data[i]==Array1[i]){ etc, as it wouldn't matter, but even so, doing what remains 64 times is giving me pause for thought!
Is there a quicker way to compare, or another way of doing it?
Thanks |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 03, 2005 4:03 am |
|
|
The data in the arrays is only containing 0 and 1 values, i.e. binary data? If yes, then storing the data as an int8 will give you an 8 fold improvement in both speed and memory usage.
The reason for strcmp to fail is probably because this function doesn't accept pointers to data in ROM (constant arrays are in ROM). You can get it working by copying the data from ROM to RAM. Or write your own strcmp function that does accept ROM data.
Quote: | I could slim it down by leaving out if(!data[i]==Array1[i]){ etc, as it wouldn't matter, but even so, doing what remains 64 times is giving me pause for thought! |
Offset calculations in an array require a lot of code, so removing this test will double the speed.
Other enhancement suggestions:
- Why always loop for all 15 items? Break the loop on first difference.
- Now you have two counters, i and Array1True. One counter will do.
- The compiler can sometimes generate more efficient code for a loop counting down to zero.
Code: | for(i=0; i <= 14; ++i)
if (data[i] != Array1[i])
break; // Quit loop on first difference
};
if (i > 14) {
printf("Data matches Array1");
}; |
|
|
|
Ttelmah Guest
|
|
Posted: Wed Aug 03, 2005 4:37 am |
|
|
The problem with strcmp, is that a '0', is seen as the end of a string.
If however you look at the source code for this (which is in string.h), and particularly at 'strncomp', whih has the following code:
Code: |
/* standard template:
int strncmp(const char *s1, const char *s2, size_t n).
Compares max of n characters (not following 0) from s1 to s2;
returns same as strcmp */
signed int strncmp(char *s1, char *s2, size_t n)
{
for (; n > 0; s1++, s2++, n--)
if (*s1 != *s2)
return((*s1 <*s2) ? -1: 1);
else if (*s1 == '\0')
return(0);
return(0);
}
|
Then simply copy this, as perhaps 'arrayncmp', but remove the line where it checks for the string value being zero, so you have:
[code]
signed int arrayncmp(char *s1, char *s2, size_t n)
{
for (; n > 0; s1++, s2++, n--)
if (*s1 != *s2)
return((*s1 <*s2) ? -1: 1);
return(0);
}
[code]
This will then give an array comparison function, which wll require pointers to the two arrays, and the size of the array as the third argument.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|