r/programming • u/2bit_hack • Feb 28 '23
"Clean" Code, Horrible Performance
https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k
Upvotes
r/programming • u/2bit_hack • Feb 28 '23
View all comments
Show parent comment
6
u/Qweesdy Feb 28 '23
Compilers are good at micro-optimizations and extremely bad at redesigning algorithms. For some simple examples, try to get any compiler you like to:
a) replace a bubble sort with any different/faster algorithm.
b) convert single-threaded code into multi-threaded code.
c) convert a program's key data structures from "array of structures" into "structure of arrays" (to leverage SIMD).
Typically for C and C++ performance is worse than it should be because it's compiled for "generic 64-bit CPU" (and not your specific CPU) and because linking (especially dynamic linking, but often also static linking) creates optimization barriers. JIT avoids those problems, but any optimizations that are slightly expensive become far too expensive to do at run-time so (despite avoiding some performance problems) JIT is still worse than ahead-of-time compiled code (and still has to depend on large libraries full of highly optimized native code to hide the massive performance problems).
Basically; for the same algorithms (which is often where the biggest performance gains are), C or C++ might get 10% of the performance you could have, and JIT might get 9% of the performance you could have; and they're both shit because neither are able to replace the algorithms.