Re: [xml] RAW && NXT with strncmp()

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

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
        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
[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 (strncmp(buffer, "xmlns", sizeof(buffer)) == 0)
      z = 1;
    if (buffer[0] == 'x' && buffer[1] == 'm' &&
        buffer[2] == 'l' && buffer[3] == 'n' &&
        buffer[4] == 's')
      z = 1;
  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
xml gnome org

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]