View previous topic :: View next topic |
Author |
Message |
rrb011270
Joined: 07 Sep 2003 Posts: 51
|
The fastest way to compare two buffers? |
Posted: Mon Mar 01, 2004 4:33 pm |
|
|
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: 1938 Location: Norman, OK
|
Compare Buffers |
Posted: Mon Mar 01, 2004 4:40 pm |
|
|
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
|
Re: Compare Buffers |
Posted: Mon Mar 01, 2004 4:50 pm |
|
|
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 |
Posted: Mon Mar 01, 2004 4:52 pm |
|
|
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
|
|
Posted: Mon Mar 01, 2004 4:59 pm |
|
|
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: 1938 Location: Norman, OK
|
Buffer compare |
Posted: Mon Mar 01, 2004 6:42 pm |
|
|
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 |
Posted: Tue Mar 02, 2004 3:42 am |
|
|
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 |
|
|
|