Re: Benchmark results (Re: Storage of char in 64 bit machine)
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 18 Aug 2006 16:44:05 -0400
Stephen Sprunk wrote:
.... snip ...
I am curious if the following has a substantially different speed
from your version:
-------
/* NOTE: c1 and c2 must point to strings of exactly 3 chars */
int
CurEqual(char *c1, char *c2)
{
/* catch people calling this function with bad params */
assert((strlen(c1)==3) && (strlen(c2)==3));
/* Note: & is safe here since == always returns 0 or 1,
and it's likely to be faster than && */
if ((c1[0]==c2[0])&(c1[1]==c2[1])&(c1[3]==c2[3]))
printf("Same currency %s\n", c1);
else
printf("%s and %s are different\n", c1, c2);
}
-------
This version looks, to me, like it's fully portable and should be
substantially faster than strcmp(), at least when NDEBUG is defined.
Lets cut that down to the essentials, and make it compilable:
int ceq(const char *c1, const char *c2)
{
return (*c1 == *c2) & (c1[1] == c2[1]) & (c1[2] == c2[2]);
}
with the same comments about & as yours. This must perform at
least 4 indexing operations, 6 dereferences, 3 comparisons, and 2
bit ands.
Now, lets analyze a normal comparison, without the limitations on
length etc. operating on the same length 3 strings:
int ceq(const char *c1, const char *c2)
{
while (*c1 && (*c1 == *c2)) {
c1++; c2++;
}
return *c1 == *c2;
}
In the best case, *c1 != *c2, assuming reasonable register
allocations, there will be 2 dereferences, one zero test, and one
comparison. The loop is never executed. Note that for random
strings this is the MOST LIKELY result. Much faster.
In the worst case, there is no difference, and all three components
have to be checked. For the same allocation assumptions, there
will be 7 dereferences, 4 zero tests, 3 comparisons, and 6 pointer
increments, followed by a final comparison.
You will have to do an unholy number of calls to these routines to
measure the difference with most clock systems resolution. Have
fun. Don't forget that execution speed is data dependant so you
will need to generate bags and bags of random strings. I suggest
the Mersenne Twister to do that, but you will have to compensate
for string generation time.
--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
.
- Follow-Ups:
- References:
- Storage of char in 64 bit machine
- From: aruna . mysore
- Re: Storage of char in 64 bit machine
- From: Lew Pitcher
- Re: Storage of char in 64 bit machine
- From: Mikhail Teterin
- Re: Storage of char in 64 bit machine
- From: Stephen Sprunk
- Benchmark results (Re: Storage of char in 64 bit machine)
- From: Mikhail Teterin
- Re: Benchmark results (Re: Storage of char in 64 bit machine)
- From: Stephen Sprunk
- Storage of char in 64 bit machine
- Prev by Date: Re: Newbie needs help with pointers/memory allocation
- Next by Date: Re: Is it declaration or defination?
- Previous by thread: Re: Benchmark results (Re: Storage of char in 64 bit machine)
- Next by thread: Re: Benchmark results (Re: Storage of char in 64 bit machine)
- Index(es):
Relevant Pages
|