Re: quickly comparing blocks of ints
- From: "rossum" <rossum48@xxxxxxxxxxx>
- Date: 27 May 2006 08:52:00 -0700
Snis Pilbor wrote:
Hello,
In a time-intensive part of my code, I manually check if two
blocks of ints (of the same size) are identical. I do it in the
straightforward way...
bool compare_int_tables( int *intpointer1, int *intpointer2 )
{
int i;
for ( i = 0; i < size; i++ )
{
if ( intpointer1[i] != intpointer2[i] )
return FALSE;
}
return TRUE;
}
My question is, is there any faster way to do this? I am not very
familiar with what various chips various processors have, but is
checking blocks of generic data like this common enough that it's worth
using a special function from some .h file to do the above in hopes
that the compiler will relegate the work to some super-fast chip, or
something?
Thanks for help with this,
S.P.
As you are just comparing for in/equality it might be worth computing a
hash value for each integer block to reduce the number of full
comparisons. Where the hash values are different there is no need to
compare the blocks - they must be different. Effectively this replaces
a full block comparison with a single integer comparison. A full
comparison is only needed where the hash values are the same because of
the possibility of collisions. You save many comparisons at the cost
of one hash computation per block.
You don't say where the integer blocks originate. If you have control
over their production then if the hash can be built as each number
block is being built then that might not add too much overhead to the
program. Even if the blocks come in from outside, each hash only has
to be calculated once while a block may have to be compared many times.
Assuming that you do more than one comparison per block this should
give a time saving. The higher the proportion of mismatches the better
the saving should be as a match will always need a full comparison.
rossum
.
- Follow-Ups:
- Re: quickly comparing blocks of ints
- From: Gene
- Re: quickly comparing blocks of ints
- References:
- quickly comparing blocks of ints
- From: Snis Pilbor
- quickly comparing blocks of ints
- Prev by Date: Re: sudoku blitz
- Next by Date: Re: Find second minimum number
- Previous by thread: Re: quickly comparing blocks of ints
- Next by thread: Re: quickly comparing blocks of ints
- Index(es):
Relevant Pages
|