Skip to content
Tcl Source Code View Ticket
Login

View Ticket

strcmp in CompareStringKeys

Closed / Fixed951168ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 19 years ago
Descriptionplain2004-05-10 10:38:01
Hi, my proposal is to use the strcmp function in tclHash.c
 for comparing two strings, instead of the custom loop: 
this should give more options to optimize the code.

Giambattista Bloisi

static int
CompareStringKeys(keyPtr, hPtr)
    VOID *keyPtr;/* New key to compare. */
    Tcl_HashEntry *hPtr;/* Existing key to 
compare. */
{
    register CONST char *p1 = (CONST char *) keyPtr;
    register CONST char *p2 = (CONST char *) hPtr-
>key.string;

if (strcmp(p1, p2) == 0) {
return 1;
}

    return 0;
}
dgp Ticket [951168ffff]: 2 changes [History diff]2004-05-10 22:17:02
dkfplain2004-05-10 22:30:12
Logged In: YES 
user_id=79902

I'm not sure how much difference that would make in
practice.  Need to take a look at some ASM output from an
optimising compiler :^/
nobodyplain2004-05-13 19:43:15
Logged In: NO 

Hi I just wrote this little performance test. On Solaris and 
Linux machine strcmp seems to be twice faster.

#define __NO_STRING_INLINES /* get optimized assembly 
version of strcmp with glibc */
#include <string.h>
#include <time.h>

int test1(const char* a, const char* b)
{
  return strcmp(a, b);
}


int test2(const char* p1, const char* p2)
{
    for (;; p1++, p2++) {
        if (*p1 != *p2) {
            break;
        }
        if (*p1 == '\0') {
            return 1;
        }
    }
    return 0;

}

int main()
{
        char a[128];
        char b[128];
        int i;
        clock_t start, end;

        strcpy(a, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        strcpy(b, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab");

        start = clock();
        for (i=0; i< 1000000;i++)
          test1(a, b);
        end = clock();
        printf("strcmp time = %d\n", end-start);

        start = clock();
        for (i=0; i< 1000000;i++)
          test2(a, b);
        end = clock();
        printf("own  time = %d\n", end-start);

        return 0;
}
dkfplain2004-05-27 17:32:58
Logged In: YES 
user_id=79902

I've been looking at this, and it *still* seems odd to me. 
Which version of which compiler are you using?  With gcc
3.2.2 on Linux, I only get optimal output if I set -O3 and
-D__USE_STRING_INLINES.
nobodyplain2004-05-27 19:43:55
Logged In: NO 

Well. I run the test on a dual Pentium-III (800Mhz) machine. 
RedHat linux 7.1. glibc 2.2.5 and both 2.95.3 and 3.4.0 gcc.
I noticed that gcc 2.95.3 produces similar results, instead 
with 3.4.0 strcmp runs faster (probably inlines optimized 
assembly code).
If you want I can try to do a complete comparition on other 
machine/compilers.

Giambattista

Here the results (I removed __NO_STRING_INLINES from the 
code):
lele@sughero[61]=> /geek-gadgets/i686-glibc22-linux/gcc-
3.4.0/bin/gcc -O3 -march=i686 strcmpr.c        
lele@sughero[62]=> a.out
strcmp time = 0
own  time = 200000
lele@sughero[63]=> /geek-gadgets/i686-glibc22-linux/gcc-
3.4.0/bin/gcc -O2 -march=i686 strcmpr.c 
lele@sughero[64]=> a.out
strcmp time = 10000
own  time = 180000
lele@sughero[65]=> /geek-gadgets/i686-glibc22-linux/gcc-
3.4.0/bin/gcc -O1 -march=i686 strcmpr.c 
lele@sughero[66]=> a.out
strcmp time = 0
own  time = 300000
lele@sughero[67]=> /geek-gadgets/i686-glibc22-linux/gcc-
3.4.0/bin/gcc -march=i686 strcmpr.c    
lele@sughero[68]=> a.out
strcmp time = 230000
own  time = 470000

lele@sughero[69]=> /geek-gadgets/i686-glibc22-linux/gcc-
2.95.3/bin/gcc -march=i686 -O3 strcmpr.c   
lele@sughero[70]=> a.out
strcmp time = 220000
own  time = 200000
lele@sughero[71]=> /geek-gadgets/i686-glibc22-linux/gcc-
2.95.3/bin/gcc -march=i686 -O2 strcmpr.c 
lele@sughero[72]=> a.out
strcmp time = 220000
own  time = 210000
lele@sughero[73]=> /geek-gadgets/i686-glibc22-linux/gcc-
2.95.3/bin/gcc -march=i686 -O1 strcmpr.c 
lele@sughero[74]=> a.out
strcmp time = 230000
own  time = 210000
lele@sughero[75]=> /geek-gadgets/i686-glibc22-linux/gcc-
2.95.3/bin/gcc -march=i686 strcmpr.c    
lele@sughero[76]=> a.out
strcmp time = 230000
own  time = 490000
dkfplain2004-05-27 22:11:35
Logged In: YES 
user_id=79902

I've updated the Tcl HEAD so that adding
-DTCL_COMPARE_HASHES_WITH_STRCMP to your build options when
building will make hashtables use strcmp() to compare C
strings using code equivalent to your suggestion.  It's not
enabled by default because I cannot duplicate your
performance gains on my test system with a full run of Tcl;
that machine is shared though, so I don't know if that's fair.

Leaving open to remind me to revisit this at some later date.
dgpplain2007-07-03 01:21:16
Logged In: YES 
user_id=80530
Originator: NO


Here's your reminder to revisit.

I'd like to see the #ifdef and
the custom code go away, and have
us call strcmp() just like the OP
here is asking.
dkfplain2007-07-03 03:31:41
Logged In: YES 
user_id=79902
Originator: NO

I still see no performance difference, even when running substantial portions of the test suite on an unloaded machine. On the other hand, I also think that cutting the clutter is a good thing... :-)

Simplified in favour of strcmp() version.
dkf Closed ticket [951168ffff]. [History diff]2007-07-03 03:31:42
Log in (anonymous is fine) to add a comment.