r/AskComputerScience • u/20260819 • Aug 19 '26
is clean code usually not fast?
to be specific i'm writing a cpu-based rasterizer. the maths are not difficult but i find a strange property: if i divide the procedure into some small functions, the code looks cleaner and is easier to maintain but a bit slower. on the contrary if i put everything into a single procedure, it looks stupid but fast. why is that? an example illustrating this
code 1:
if cross_product(x0,y0,x1,y1)>0 then zzz
(and i write a "cross_product" function separately)
code 2:
c=x0y1-y0x1
if c>0 then zzz
code 3:
if x0y1-y0x1>0 then zzz
if i write the entire algorithm in the style of "code 3", it runs the fastest. "code 1" is slowest
is it normal?
15
u/[deleted] Aug 19 '26 edited Aug 19 '26
Function calls and variable assignments aren't free, both take time to complete. However, this is going to depend on the programming language you use. For example, C compilers are very good at compiling C code into really efficient assembly, so you can write code in whatever way makes sense to you and little things like this will be optimized away. If you're using an interpreted language like Python, then this code will be interpreted much more literally and it has the double whammy of the inherent slowness of the Python interpreter and its dynamic type system.
Having said all that, I would consider these micro-optimizations. It is an interesting observation that the function call is slower, but it is usually better to care about readability and larger optimizations first, then take care of these if you need to squeeze out every last bit of performance.
Edit: What I said about C doesn't apply if you don't have optimizations on (i.e., if you aren't compiling with an -O flag). But based on your pseudocode I assumed you aren't using C as the variables aren't typed.
2
u/ImpressiveOven5867 Aug 20 '26 edited Aug 20 '26
So I saw in your comment that you are using QBJS “for sake of convenience and ease to share” which is the craziest thing I ever heard in my life.
QBJS is a source to source compiler from QBasic to JS basically, but the way the compiler is designed makes it such that every user function becomes asynchronous. That means each of your functions goes through the whole async/await process, which makes it 7-8x slower than if it was written the same way in plain JS (which is already slow lol).
So basically yes, in this specific case “clean code” is directly making your program slower because of the way the compiler works. Almost all compilers don’t work like this though and just call a function or inline it, so in general clean code does not make code slower.
5
u/ICantBelieveItsNotEC Aug 19 '26
It depends on the language and on the compiler. If the compiler is halfway decent, your functions should get inlined automatically, and they should all compile down to roughly the same bytecode.
1
u/two_three_five_eigth Aug 22 '26
code 1 has a function call. Calling the function (unlesss it's inlined) means the variables get copied, so 4 extra copies.
Code 2 seems equivalent to code 3, but you've added an extra variable that lives to the end of the block.
Code 3 the calculation is scoped to exactly where it's needed, so the compiler has no trouble optimizing it.
Without digging further, likely 3 is fastest because you did exactly what you needed, which let the compiler optimize it the best.
3
u/coterminous_regret Aug 19 '26
No not at all. What compiler optimization settings are you using? Is your cross product function marked as inline? Or force inline via a compiler macro?
The higher level language representation may not at all reflect the code that the compiler generates. You may enjoy exploring a tool like https://godbolt.org/ where you can try stuff out with various optimization settings and languages to see what the assembler actually looks like.
1
u/Leverkaas2516 Aug 19 '26
It often does carry some execution-time overhead like this, yes. The idea is this: most of your code does not need to be optimized for speed. If you optimize while writing it, you're wasting resources and making the code hard to work with.
Once you figure out where speed matters, if the code is clean, then it's a lot easier to understand how to change the parts that need to be optimized. You end up with clean, optimized code and finish the development work faster.
If you try to optimize up front, you take longer to finish and end up with 20k lines of code that has hidden flaws and most of the team is scared to touch. (I just did a code review on a change to such a module yesterday.)
1
u/koorb Aug 19 '26
Slower in execution, but faster to maintain, fix and extend.
1
u/Cool_Homework_7411 Aug 20 '26
The machine only understands 0s and 1s. The compiler is the one who takes your language and makes it computer language. If you are translating in Google word by word will you get the same thing as translating a whole sentence? Probably not and you can see why. "Breaking" your code in pieces makes it harder for the compiler to translate effectively what you want to machine language
1
u/R2-Scotia Aug 20 '26
inline
1
u/WellHung67 Aug 22 '26
Use compiler explorer and see the difference
1
u/ka-splam Aug 23 '26 edited Aug 23 '26
I think this is an unresolved tension in programming, driven by several valid ideas clashing, and people thinking about different contexts:
Casey Muratori argues that the industry has gone too far towards 'clean code' abstractions, and has video rants on the topic: "Clean code, horrible performance" and Twitter and Visual Studio Rant where he demonstrates that the visual studio debugger of old was more responsive on 20 year old hardware than the current one is today on modern hardware, and the visual studio team have a feedback option "opening an empty workspace should take X seconds" which has no choice below 10 seconds.
As I said I don't think this tension has any resolution. You could ask yourself if
is helping in any way that a comment
isn't. And sometimes you will say "yes it helps me refactor the cross product without missing one place, it helps my tooling and editor autocomplete, it helps avoid code and comments getting out of sync" and sometimes you will say "splitting it out into a separate function is decorative make-work that feels productive but takes time and doesn't gain anything I care about while slowing my code down".
But if your wider system design has every display call going over a network link then the difference in cross product performance might not matter at all. But if your LLM is zillions of linear algebra calculations and every nanosecond counts, then you'll want to spend more effort than removing the function and inlining the calculation in BASIC. Everything is a design tradeoff 🤷♂️