On Fri, Dec 09, 2005 at 11:56:14PM +1100, Ken Foskey wrote:
> This is an unexpected statistic...
> 
> Subroutine using massive number of matches:
>  strcmp(x,y) 1.87 seconds
>  strncmp(x,y,6) 1.63 seconds
>  memcmp(x,y,6) 5.85 seconds
> 
> Ignoring the other code it is a huge overhead for using memcmp on Ubuntu
> I386 as opposed to strncmp.
> 
> I would not have expected this, any ideas?

I think chances are you've got the gcc builtin memcmp there, which
looks pretty slow compared to the highly optimised glibc one.

See below, where the gcc one comes down to repz cmpsb which is
notoriously slow.  -fno-builtins turns it off.

e.g.

[EMAIL PROTECTED]:/tmp$ cat test.c
#include <string.h>
extern int *i, *j;

int memcmptest(void)
{
        return  memcmp(i,j,1000);
}
[EMAIL PROTECTED]:/tmp$ gcc -c test.c
[EMAIL PROTECTED]:/tmp$ objdump --disassemble ./test.o

./test.o:     file format elf32-i386

Disassembly of section .text:

00000000 <memcmptest>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   57                      push   %edi
   4:   56                      push   %esi
   5:   83 ec 0c                sub    $0xc,%esp
   8:   8b 15 00 00 00 00       mov    0x0,%edx
   e:   a1 00 00 00 00          mov    0x0,%eax
  13:   89 45 f4                mov    %eax,0xfffffff4(%ebp)
  16:   89 55 f0                mov    %edx,0xfffffff0(%ebp)
  19:   c7 45 ec e8 03 00 00    movl   $0x3e8,0xffffffec(%ebp)
  20:   fc                      cld
  21:   8b 75 f4                mov    0xfffffff4(%ebp),%esi
  24:   8b 7d f0                mov    0xfffffff0(%ebp),%edi
  27:   8b 4d ec                mov    0xffffffec(%ebp),%ecx
  2a:   f3 a6                   repz cmpsb %es:(%edi),%ds:(%esi)
  2c:   0f 97 c2                seta   %dl
  2f:   0f 92 c0                setb   %al
  32:   88 d1                   mov    %dl,%cl
  34:   28 c1                   sub    %al,%cl
  36:   88 c8                   mov    %cl,%al
  38:   0f be c0                movsbl %al,%eax
  3b:   83 c4 0c                add    $0xc,%esp
  3e:   5e                      pop    %esi
  3f:   5f                      pop    %edi
  40:   5d                      pop    %ebp
  41:   c3                      ret
[EMAIL PROTECTED]:/tmp$ gcc -fno-builtin -c test.c
[EMAIL PROTECTED]:/tmp$ objdump --disassemble ./test.o

./test.o:     file format elf32-i386

Disassembly of section .text:

00000000 <memcmptest>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 08                sub    $0x8,%esp
   6:   a1 00 00 00 00          mov    0x0,%eax
   b:   8b 15 00 00 00 00       mov    0x0,%edx
  11:   83 ec 04                sub    $0x4,%esp
  14:   68 e8 03 00 00          push   $0x3e8
  19:   50                      push   %eax
  1a:   52                      push   %edx
  1b:   e8 fc ff ff ff          call   1c <memcmptest+0x1c>
  20:   83 c4 10                add    $0x10,%esp
  23:   c9                      leave
  24:   c3                      ret
[EMAIL PROTECTED]:/tmp$

-i

Attachment: signature.asc
Description: Digital signature

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to