strcmp a better way?

This is a discussion on strcmp a better way? within the C++ Programming forums, part of the General Programming Boards category; Ok i need to know if there is something better than strcmp. i have 2 strings that contain 50,000 charictors ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    58

    strcmp a better way?

    Ok i need to know if there is something better than strcmp. i have 2 strings that contain 50,000 charictors (no there is no way i could make it less) and when they are being compared it takes a LONG time. So long in fact i've never stuck around to find out how long it actually takes. Help is much apreciated.
    --== www.NuclearWasteSite.com==--

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Try using memcmp as you know it has 50000 charecters.
    50,000 doesn't sound like it would be too long. Here's some
    code I tested strcmp on, it only takes a instant on a pentium 2.

    Code:
    include <iostream>
    using std::cout;
    using std::endl;
    
    #include <cstring>
    using std::memset;
    using std::strcmp;
    
    int main()
    {
        char s1[50000];
        char s2[50000];
    
        memset(s1, 'A', sizeof s1 - 1);
        memset(s2, 'A', sizeof s2 - 1);
    
        s1[49999] = '\0';
        s2[49999] = '\0';
    
        cout << "strcmp(s1, s2) = " << strcmp(s1, s2) << endl;
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    I was curious to see if there was any way to do strcmp in ASM other than the method that the standard strcmp uses. It seems that the ASM operation "cmpsb" can be useful for quickly comparing strings (and strcmp doesn't use it), but I'm not having too much luck with it. Here's some code; maybe someone can figure out what's wrong with it and help you:

    Code:
    bool asmstrcmp(char * str1, char * str2)
    {
    	bool result = 1;
    	
    	_asm
    	{
    		lds si, [str1]
    		les di, [str2]
    		std
    		mov cx, 10
    		rep cmpsb
    		jne notequal
    		jmp end
    notequal:
    		mov result, 0
    end:
    	}
    	
    
    	return result;
    }
    Even though that code doesn't work, I hope it'll be helpful. I think it will be if someone can tell me what's wrong with it.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    EDIT: That should be "sld" instead of "std" in the above ASM.

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    There's something wrong with your strings. Maybe you
    forgot the null charecter or something? One micro-optmization
    that I've seen is

    if (*s1++ != *s2++)
    if (!strcmp(s1, s2)) {
    /* strings are equal */
    }
    }

    I can't remark on how effective this is. Your choice
    of algorithm for the sort or whatever will matter more
    than saving a function call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21