-
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.
-
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;
}
-
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.
-
EDIT: That should be "sld" instead of "std" in the above ASM.
-
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.