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

The fastest way to compare two buffers?

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



Joined: 07 Sep 2003
Posts: 51

View user's profile Send private message Yahoo Messenger

The fastest way to compare two buffers?
PostPosted: Mon Mar 01, 2004 4:33 pm     Reply with quote

Hello,

Mabuhay!

Anyone from the community has an algorithm that compares two buffers (say Temp1[16] and Temp2[16])? I mean the fastest way to compare two buffers or arrays?

Need help..

Thanx
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

Compare Buffers
PostPosted: Mon Mar 01, 2004 4:40 pm     Reply with quote

Assuming the two buffers are laid out identically the simplest way is to set pointers at the start of each one. Then set up a simple loop to increment and compare locations at each pointer directly until a mismatch occurs. testing the loop counter on exit will tell you if a match fail occurred before you reached the end and exactly where.
rrb011270



Joined: 07 Sep 2003
Posts: 51

View user's profile Send private message Yahoo Messenger

Re: Compare Buffers
PostPosted: Mon Mar 01, 2004 4:50 pm     Reply with quote

dyeatman wrote:
Assuming the two buffers are laid out identically the simplest way is to set pointers at the start of each one. Then set up a simple loop to increment and compare locations at each pointer directly until a mismatch occurs. testing the loop counter on exit will tell you if a match fail occurred before you reached the end and exactly where.


Can I have a sample snippet on this method?

Thnx
Ttelmah
Guest







Re: Compare Buffers
PostPosted: Mon Mar 01, 2004 4:52 pm     Reply with quote

dyeatman wrote:
Assuming the two buffers are laid out identically the simplest way is to set pointers at the start of each one. Then set up a simple loop to increment and compare locations at each pointer directly until a mismatch occurs. testing the loop counter on exit will tell you if a match fail occurred before you reached the end and exactly where.

It really does depend on the nature of the buffers, and the comparison required. Though pointer/array accesses, make the coding easy, and will generate efficient 'general purpose' comparisons (remember the strcmp function does exactly this for you), it is significantly faster to do a direct 'byte' comparison from a fixed address. The downside is that the code is less general purpose, and bulkier.
So (for instance), if you are only comparing a short buffer, and need to exit with a true/false result, something like:
Code:

int8 result;

result=false;
if (buffer1[0] == buffer2[0])
   if (buffer1[1] == buffer2[1])
      if (buffer1[2] == buffer2[2])
         if (buffer1[3] == buffer2[3]) result=true;
//Here result will be true if four charachters match.

Though incredibly ugly in a sense, this gives very fast code, since addresses with a 'constant' offset, are translated into direct fetches. Hence this will give a four byte comparison of two arrays, in about the shortest time possible.

Best Wishes
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Mon Mar 01, 2004 4:59 pm     Reply with quote

This is as fast as you can get for raw speed and is not very large. Try this and do a listing. This is really just a different way to syntax what Ttelmah just posted. They compile the same.
Code:

Int1 They_Match;
   They_Match = ((Buffer_0[0] == Buffer_1[0])
              && (Buffer_0[1] == Buffer_1[1])
              && (Buffer_0[2] == Buffer_1[2])
              && (Buffer_0[3] == Buffer_1[3])
              && (Buffer_0[4] == Buffer_1[4]));
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

Buffer compare
PostPosted: Mon Mar 01, 2004 6:42 pm     Reply with quote

What I had in mind was a little different and is quite a bit more concise for a larger array pair... Whatever works for you.

ptr1=&Temp1[0];
ptr2=&Temp2[0];

for(i=0;i<16;i++)
if(*Ptr1++!=*Ptr2++)
{
error=1;
break;
}
Ttelmah
Guest







Re: Buffer compare
PostPosted: Tue Mar 02, 2004 3:42 am     Reply with quote

dyeatman wrote:
What I had in mind was a little different and is quite a bit more concise for a larger array pair... Whatever works for you.

ptr1=&Temp1[0];
ptr2=&Temp2[0];

for(i=0;i<16;i++)
if(*Ptr1++!=*Ptr2++)
{
error=1;
break;
}

What you post is fine (assuming in the example given, that the buffers are 16 characters long). The problem I was alluding to, is that the processor takes a relatively long time to do 'pointer' (and array using variables) accesses.
Basically, it has to transfer Ptr1, into the index registers, then fetch the byte from the addressed register. Then repeat this for Ptr2. You will find that a single byte comparison, will probably take about 30 instruction times. The 'fixed address' comparison (either using nested if's or the logical && operation), instead only takes about 6 instruction times per character.
As I said, it does depend on the nature of the buffers, the exact comparison needed, and how important speed really is.

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