“Clean” Code, Horrible Performance
"Clean" Code, Horrible Performance (2023)
In this video from the Performance-Aware Programming series, Casey Muratori demonstrates how following “clean code” guidelines—like preferring polymorphism and hiding internals—can lead to significant performance costs. Using the example code from clean code literature, he shows that a polymorphic shape hierarchy runs at ~35 cycles per shape, while a simple switch-based approach runs at ~24 cycles, a 1.5x speedup. He argues that violating these rules can yield even greater gains, and that the clean code emphasis on abstraction often obscures optimization opportunities.
So by violating the first rule of clean code — which is one of its central tenants — we are able to drop from 35 cycles per shape to 24 cycles per shape, implying that code following that rule is 1.5x slower than code that doesn’t.