String functions are a prevalent bottleneck in high performance routines, due to which functions like C's strcmp
are written in
hand optimized assembly.
strcmp
quits as soon as a character mismatch is detected in the string. The following loop is used to benchmark its performance:
volatile int common_strings = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (!strcmp(strs[i], strs[j]))
common_strings++;
Runtime for N = 50000: 11.24 seconds
While it is performant, if the hit rate is low (eg - associative array), a simple first char check ends up giving a 2x performance boost:
volatile int common_strings = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (*strs[i] == *strs[j] && !strcmp(strs[i], strs[j]))
common_strings++;
Runtime for N = 50000: 4.84 seconds