<story><title>Why is this C++ code faster than my hand-written assembly (2016)</title><url>https://stackoverflow.com/questions/40354978/why-is-this-c-code-faster-than-my-hand-written-assembly-for-testing-the-collat</url></story><parent_chain><item><author>payne92</author><text>tl;dr -- the asm author used DIV to divide by a constant 2&lt;p&gt;More fundamentally:  it&amp;#x27;s theoretically possible to at least match compiled code performance with assembly, because you could just write the code the compiler generates.&lt;p&gt;BUT, it requires a LOT of experience.&lt;p&gt;Modern compilers &amp;quot;know&amp;quot; a lot of optimizations (e.g. integer mult by fixed constant --&amp;gt; shifts, adds, and subtracts).  Avoiding pipeline stalls requires a lot of tedious register bookkeeping, and modern processors have very complicated execution models.&lt;p&gt;It&amp;#x27;s almost always better to start with a compiler-generated critical section and see if there are possible hand optimizations.</text></item></parent_chain><comment><author>bradford</author><text>In University computer architecture courses, we were challenged to write a quicksort routine in assembly.  We were also asked to compare the assembly that we authored with assembly that was compiled from C++ (after we authored our own solutions, of course).&lt;p&gt;It was an amazing crash-course on just how good compilers have become at optimizing.  Not a single student could hand craft assembly that was faster than the compiler output.  The teacher of the course was able to generate assembly that was slightly faster, and he stated that in order to do so, he had to greatly exploit his in-depth knowledge of the processor&amp;#x27;s pipeline system.  That was roughly year 2000, and I&amp;#x27;m sure compilers have only become better at their job since then.&lt;p&gt;All in all, excellent learning experience.  I&amp;#x27;ve since encountered several instances where developers assert superior assembly skills, and by default I&amp;#x27;m silently skeptical of their claims.</text></comment>
<story><title>Why is this C++ code faster than my hand-written assembly (2016)</title><url>https://stackoverflow.com/questions/40354978/why-is-this-c-code-faster-than-my-hand-written-assembly-for-testing-the-collat</url></story><parent_chain><item><author>payne92</author><text>tl;dr -- the asm author used DIV to divide by a constant 2&lt;p&gt;More fundamentally:  it&amp;#x27;s theoretically possible to at least match compiled code performance with assembly, because you could just write the code the compiler generates.&lt;p&gt;BUT, it requires a LOT of experience.&lt;p&gt;Modern compilers &amp;quot;know&amp;quot; a lot of optimizations (e.g. integer mult by fixed constant --&amp;gt; shifts, adds, and subtracts).  Avoiding pipeline stalls requires a lot of tedious register bookkeeping, and modern processors have very complicated execution models.&lt;p&gt;It&amp;#x27;s almost always better to start with a compiler-generated critical section and see if there are possible hand optimizations.</text></item></parent_chain><comment><author>krylon</author><text>In a prior job I worked as a C programmer, and once I tried to rewrite  a few heavily used utility function in assembly.&lt;p&gt;I measured, but it did not run any faster. Damnit, this is assembly, I said, it &lt;i&gt;has&lt;/i&gt; to be faster. So I looked at the assembly code generated by the compiler: Turned out it was pretty much identical to the code I had written. At that point, I felt brief surge of pride (because I was as clever as the compiler) and then disappointment (because I was not more clever than the compiler), and I figured trying to be smarter than the compiler was a waste of my time.</text></comment>