Re: [xml] RAW && NXT with strncmp()
- From: Chris Anderson <christop charm net>
- To: xml gnome org
- Subject: Re: [xml] RAW && NXT with strncmp()
- Date: 01 Oct 2003 14:26:30 -0400
On Wed, 2003-10-01 at 12:47, Bjorn Reese wrote:
On Wed, 2003-10-01 at 15:50, Daniel Veillard wrote:
That I was looking at it this yesterday actually but I don't feel
that confident about such a change, yet ...
Not to belabor this point too much, it's not that important...
There are a couple of things going on in that loop:
First, icc completely optimizes the loop away, gcc just optimizes away
the z = 1 statement since it's not used.
Second, gcc puts the all of the characters of "xmlns" into registers
before the loop begins, so the no strncmp test is really testing the
speed of 5 * 1000000000 register compares and branches, which is a bit
faster than loading the constants into registers and comparing.
Third, the other point was that there are cases where most of the
strings are not equal, this loop just tests the case where they are
equal.
Fourth, strncmp is not an intrinsic in either gcc or icc. Use memcmp,
that will generate x86 repz string instructions as long as the size of
the memory being compared is constant (size == 5).
no memcmp:
cmpb $120, -73(%ebp)
jne .L128
cmpb $109, %bl
jne .L128
cmpb $108, %cl
jne .L128
cmpb $110, %dl
memcmp( buffer, "xmlns", 5 )
leal -88(%ebp), %esi
movl $.LC0, %edi
movl $5, %ecx
cld
repz
cmpsb
seta %dl
setb %al
cmpb %al, %dl
The no memcmp is quite a bit faster, but the instruction count isn't
that different. I think it's more of a clarity thing unless you are in
a code hot spot.
The libc string functions tend to be highly optimized.
Time for informal benchmarking...
[breese stellifer:/home/breese/junk] gcc --version | head -1
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
[breese stellifer:/home/breese/junk] uname -a
Linux stellifer 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386
GNU/Linux
[breese stellifer:/home/breese/junk] cat compare.c
#include <string.h>
int main(void)
{
int i;
int z;
char buffer[64] = "xmlns";
for (i = 0; i < 1000000000; ++i) {
#if USE_STRNCMP
if (strncmp(buffer, "xmlns", sizeof(buffer)) == 0)
z = 1;
#else
if (buffer[0] == 'x' && buffer[1] == 'm' &&
buffer[2] == 'l' && buffer[3] == 'n' &&
buffer[4] == 's')
z = 1;
#endif
}
return 0;
}
[breese stellifer:/home/breese/junk] gcc -O2 compare.c
[breese stellifer:/home/breese/junk] time ./a.out
4.560u 0.040s 0:04.85 94.8% 0+0k 0+0io 65pf+0w
[breese stellifer:/home/breese/junk] gcc -DUSE_STRNCMP -O2 compare.c
[breese stellifer:/home/breese/junk] time ./a.out
0.930u 0.000s 0:01.03 90.2% 0+0k 0+0io 66pf+0w
I get roughly the same results if I change the content of 'buffer'.
_______________________________________________
xml mailing list, project page http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]