|
|
|
|
|
by utopcell
1073 days ago
|
|
Indeed. I suppose the two lessons are, stick with C, and don't forget the semantics of your original problem when optimizing. int run_switches(const char *s) {
int res = 0;
uint8_t tmp = 0;
size_t n = strlen(s);
for (size_t i = n & 127; i--; ++s)
tmp += (*s == 's');
res += tmp;
for (size_t j = n >> 7; j--;) {
tmp = 0;
for (size_t i = 128; i--; ++s)
tmp += (*s == 's');
res += tmp;
}
return 2 * res - n;
}
|
|
Edit: Also, there's an off by one error. should be:
~90GB/s on my machine, compared to 4.5GB/s for his best effort on his blog. So 20x as fast.