CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Fast way of checking array?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bwgames



Joined: 21 Jul 2005
Posts: 36

View user's profile Send private message

Fast way of checking array?
PostPosted: Wed Aug 03, 2005 3:05 am     Reply with quote

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 Smile
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Aug 03, 2005 4:03 am     Reply with quote

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







PostPosted: Wed Aug 03, 2005 4:37 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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