5 ms·
General comment and not aimed at this specific instance: Just because you are writing in assembler, does not mean it is going to run faster than the same code
by SeanDav 9y ago
General comment and not aimed at this specific instance:
Just because you are writing in assembler, does not mean it is going to run faster than the same code in a compiled language. There has been decades of research and who knows how many man-years of effort that has gone into producing efficient compiled code from C, C++, Fortran etc.
Your assembly skills have to be of quite a decent order to beat a modern compiler.
BTW: The answer to the question on Stack Overflow by Peter Cordes is a must-read. Brilliant.
- Veedrac 9y agoQuite decent is perhaps a bit pessimistic. Yes, you need to be fairly fluent if you want to write hand-optimised vector code. On the other hand, you only need to know a little assembly to be able to write C code that compiles better; that compilers are frequently not stupid doesn't mean the level below has nothing to teach. I personally find it a shame that there are so many juicy instructions that compilers have no hope of ever using effectively. How often is a compiler smart enough to solve a problem with PDEP/PEXT, for example? Those functions are versatile as heck, but you need to plan for them if you want them to show up.
- 15155 9y ago> Your assembly skills have to be of quite a decent order to beat a modern compiler. Start with compiler output, add intrinsics where you can see help is needed, benchmark, repeat. Pathologically-slow ASM is pretty rare from modern compilers in my experience.
- jzwinck 9y ago> Pathologically-slow ASM is pretty rare from modern compilers Here are some I've found: https://stackoverflow.com/questions/45496987/gcc-optimizes-fixed-range-based-for-loop-as-if-it-had-longer-variable-length https://stackoverflow.com/questions/45496987/gcc-optimizes-f... (horrific codegen for known-size C++11 loops in member functions, all GCC versions prior to 8 which is not yet released) https://stackoverflow.com/questions/43651923/gcc-fails-to-optimize-aligned-stdarray-like-c-array https://stackoverflow.com/questions/43651923/gcc-fails-to-op... (SIMD opportunity squandered when C++11 features are used) https://stackoverflow.com/questions/42263537/gcc-sometimes-doesnt-inline-stdarrayoperator https://stackoverflow.com/questions/42263537/gcc-sometimes-d... (failure to inline trivial operators) https://stackoverflow.com/questions/26052640/why-does-gcc-implement-isnan-more-efficiently-for-c-cmath-than-c-math-h https://stackoverflow.com/questions/26052640/why-does-gcc-im... (C isnan() not efficient, for many years)
- acdha 9y agoThere's a key corollary: you also either have to be doing this as a one-off or committed to maintaining it over time as hardware changes and those cool tricks become moot or even de-optimizations.
- mjw1007 9y agoAlso as your code changes. It's often not that hard to hand-write a single function a bit better than the compiler. But when you want to make a small change the compiler will rethink the whole function from scratch, and if you want to keep your advantage you may have to do the same.
- acdha 9y agoThat's a very important point: this is especially the case when, say, a data structure grows and clever packing or alignment tricks can no longer be used. I certainly remember that being used as an excuse to delay a change because it was much harder to update some complex optimizations — and at least one case where the original developer tested the generic .c file they'd used to develop the algorithm with a current compiler and found it was faster on the latest hardware we were buying.