Worries about strcmp() - this is micro-optimization most of the time - it’s not worth the effort. There are other things that can be more secure, for example:
for (i = 0; i < strlen(s); i++) { ...do stuff with s[i]... }
The optimizer may not understand that he can and should avoid calling the function at each iteration of the loop - if you increase s in the loop, this may not be avoided. It is expensive.
Once strstr() time, I used a function like strstr() on buffers of 20 KB or so, and the program worked perfectly on the HP box where I developed it. I put it back on the Sun machine (remember that it was 20 years ago - the problems were solved long ago), and the program did not even crawl - it was almost stationary. The problem turned out to be a loop in strstr() , which used strlen() more or less, as shown above. When used on short buffers there was no big problem; when using 20 KB buffers, searching for a pattern that appears every couple of kilobytes, the bad machine section stops. The problem was shown by profiling the application. I included the substitute strstr() , which avoided the error, and everything returned to normal.
Another common source of slowness when porting to excess is the use of strcat() . For instance:
strcpy(dst, name); strcat(dst, "/subdir/"); strcat(dst, file); strcat(dst, ".sfx");
This is usually not a source of performance problems, but in the absence of evidence to the contrary, it can be a source of buffer overflows. You should know that the lengths are small enough to fit in dst . But, if you know the lengths of each bit (how you should be sure that they fit), you can write instead:
strcpy(dst, name); dst += len_name; strcpy(dst, "/subdir/"); dst += sizeof("/subdir/") - 1; strcpy(dst, file); dst += len_file; strcpy(dst, ".sfx");
This allows you to repeatedly scan a string of known length to find the end before adding new material. With short strings, it doesn't really matter; with long strings and many concatenation operations, this can make a difference. As before, the key is measuring value.
Kernighan and Pike have an interesting story on how to improve the performance of the spam filter in the book “T “ Programming Practices. ” He started using strstr() in a loop; it turned out to be completely different because the circumstances for which strstr() , didn’t meet the requirements of their spam filtering system, but, again, they only did this work because it was demonstrated that there was a performance problem and they did enough work so that the program was not a bottleneck in the system, but no more .