Stephen Kratzer wrote: > > In terms of speed what the fastest way of doing a strings comparison > > on fixed strings? Using one of those strcmp() functions or doing the > > following: > > > > (Pseudo code) > > "if (s[0] == 0xa && s[1] == 0xb && s[2] == 0xc)" (etc) > > or > > "if (s[0] == 'A' && s[1] == 'B' && s[2[ == 'C')" (etc) > > > > Thanks for your help. > > The function strcmp uses a do/while loop, a conditional to test every > character in the first string against the null character, and pointer > arithmetic, so, in terms of sheer speed, your method would probably be > faster. But, in terms of program clarity, strcmp wins by a mile. 1. The test against the NUL byte can't be eliminated unless you know in advance that the string is long enough. 2. The above pseudo-code also does pointer arithmetic: "s[2]" is just another way of writing "*(s+2)". 3. An actual loop is typically quicker than an "unrolled" loop (where you just repeat the loop body) as it results in smaller code and thus better cache coherency. All things considered, strcmp() is likely to be faster. -- Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx> - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html