I currently have this kind of loop
while(1)
{
generate_string(&buffer);
for(int i = 0; i < filelines; i++)
{
if(strcmp(buffer,line[i]) == 0)
{
/* do something */
}
}
}
I have a file with a few million strings(which hopefully should be cut by half sometime soon), the number of all these strings is stored in filelines
line[i] is basically where the string itself is stored.
Currently, due to the comparison of these million strings, function generate_string(&buffer); is executed around 42 times per second.
Is there a faster way to do string comparison in C?
strcmpis usually optimized by all vendors. However, if you’re not satisfied with this you can try:libcused to have this optimization for small strings where they tested strings smaller than five bytes as integers. MSclalso has some optimizations for small-strings (do look it up).But more importantly make sure
strcmpis your real bottleneck.