{n} times faster than C
IMHO the original code wasn&#x27;t written in a way that&#x27;s particularly friendly to compilers. If you write it like this:<p><pre><code>    int run_switches_branchless(const char* s) {
        int result = 0;
        for (; *s; ++s) {
            result += *s == &#x27;s&#x27;;
            result -= *s == &#x27;p&#x27;;
        }
        return result;
    }
</code></pre>
...the compiler will do all the branchless sete&#x2F;cmov stuff as it sees fit. It will be the same speed as the optimized assembly in the post, +&#x2F;- something insignificant. However it won&#x27;t unroll and vectorize the loop. If you write it like this:<p><pre><code>    int run_switches_vectorized(const char* s, size_t size) {
        int result = 0;
        for (; size--; ++s) {
            result += *s == &#x27;s&#x27;;
            result -= *s == &#x27;p&#x27;;
        }
        return result;
    }
</code></pre>
It will know the size of the loop, and will unroll it and use AVX-512 instructions if they&#x27;re available. This will be substantially faster than the first loop for large inputs, although I&#x27;m too lazy to benchmark just how much faster it is.<p>Now, this requires knowing the size of your string in advance, and maybe you&#x27;re the sort of C programmer who doesn&#x27;t keep track of how big your strings are. I&#x27;m not your coworker, I don&#x27;t review your code. Do what you want. But you really <i>really</i> probably shouldn&#x27;t.<p><a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;rde51zMd8" rel="nofollow noreferrer">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;rde51zMd8</a>