{n} times faster than C
I&#x27;m not so sure that the right take-away is &quot;hand-written assembler is 6x faster than C.&quot; It&#x27;s more like &quot;jumps are a lot slower than conditional arithmetic.&quot; And that can [edit:often] be achieved easily in C by simply not using switch statements when an if statement or two will work fine.<p>Rewriting the C function as follows got a 5.5x speedup:<p><pre><code>    int run_switches(char *input) {
        int r = 0;
        char c; 
        while (1) {
            c = *input++;
            if (c == &#x27;s&#x27;) r++;
            if (c == &#x27;p&#x27;) r--;
            if (c == &#x27;\0&#x27;) break;
        }
        return r;
    }
</code></pre>
Results:<p><pre><code>    [16:50:14 user@boxer ~&#x2F;looptest] $ gcc -O3 bench.c loop1.c -o lone
    [16:50:37 user@boxer ~&#x2F;looptest] $ gcc -O3 bench.c loop2.c -o ltwo
    [16:50:47 user@boxer ~&#x2F;looptest] $ time .&#x2F;lone 1000 1
    449000
    .&#x2F;lone 1000 1  3.58s user 0.00s system 99% cpu 3.589 total
    [16:50:57 user@boxer ~&#x2F;looptest] $ time .&#x2F;ltwo 1000 1
    449000
    .&#x2F;ltwo 1000 1  0.65s user 0.00s system 99% cpu 0.658 total</code></pre>