unsigned sum(const unsigned char *s) { unsigned result = 0; size_t length = strlen(s); for (size_t i=0; i < length; i++) { result += s[i]; } return result; }
unsigned sum(const unsigned char *s) { unsigned result = 0; size_t length = strlen(s); for (size_t i=0; i < length; ++i) // prefix increment should be quicker. result += s[i]; return result; }
I would write the loop as the optimised version first hand, especially if there were nested loops.
No. 5 caught me out, but I'm not surprised.