Stephen Sprunk wrote:
The implementation is likely to have a very, very clever strcmp() that
will perform at least as well as your code (possibly doing the same thing
internally, if it's known to be safe) and likely even better if the
compiler is reasonably modern due special knowledge and treatment of
common functions/idioms.
Well, here are some benchmarks comparing the use of strcmp() to compare
short character strings (4 characters).
FreeBSD6.1/i386 using `gcc version 3.4.4 [FreeBSD] 20050518'
with "-pedantic -Wall -O5 -pipe -march=pentium4 -DNDEBUG -DNODEBUG
-fomit-frame-pointer":
./bench 100000000
str: used 1119486 microseconds
int: used 406449 microseconds
FreeBSD6.1/amd64 using the same compiler as above
with "-pedantic -Wall -O5 -pipe -march=opteron -DNDEBUG -DNODEBUG
-fomit-frame-pointer":
obj.amd64/bench 100000000
str: used 1403187 microseconds
int: used 392897 microseconds
AIX5.2/powerpc using IBM's cc with cc -O3 (-O4 would not link):
./aix-bench 100000000
str: used 3240000 microseconds
int: used 630000 microseconds
Solaris8/sparc using Sun's 6u2 compiler with `-v -fast' (fast is the "macro"
option turning on all possible optimizations including Sun's own libmil):
./sun4u-bench 100000000
str: used 7020000 microseconds
int: used 1300000 microseconds
the same using 64-bit binaries (-v -fast -xarch=v9b):
str: used 7920000 microseconds
int: used 1470000 microseconds
Solaris10/opteron using Sun's `cc: Sun C 5.7 2005/01/07' compiler with
`-fast -xarch=amd64':
./sunx86-bench 100000000
str: used 962088 microseconds
int: used 319509 microseconds
Of the above, the Sun's cc/libmil is definitely has the special "knowledge
and treatment of common functions/idioms" such as strcmp(), but even there
using strcmp() was 5 times slower...
It seems, that for the limited cases like this -- when the strings are of
the same length and fit nicely into an integer type -- treating them as such
is hugely beneficial. And, contrary to authoritative assertions posted in
this thread, compiler is NOT able to detect such cases.
I'm attaching (sigh) the simple-minded C-code to this posting -- please,
poke at it and/or reproduce my results... It even allows for CHAR_BIT to be
16 :-)
Thanks!
-mi