r/C_Programming • u/Kabra___kiiiiiiiid • Jun 18 '26
Article Premature Optimization is Fun Sometimes
https://invlpg.com/posts/2025-06-19-premature-optimization.html
93
Upvotes
r/C_Programming • u/Kabra___kiiiiiiiid • Jun 18 '26
108
u/simonask_ Jun 18 '26
It's fun all the time. That's the problem with it.
36
u/Modi57 Jun 18 '26
Haha, yes, number go down, fill void inside
22
u/ICBanMI Jun 18 '26
*puts on nerd glasses* It's not premature optimization if you're doing it long after it's in use.
It's premature optimization if you were writing the function for the first time and went straight to trying to optimize the amount of total instructions. Bonus points if you decided some crazy data structure, that's not common use, was what you needed.
It's not premature optimization to remove padding or give hints to the compiler what route would be quicker (most common path being the first path in a control structure).
This was a fun read.
20
u/timonix Jun 18 '26
Premature optimization is a way of life. I condone it fully
15
u/apnorton Jun 18 '26
I am loathe to let a reference to "premature optimization" pass without pointing out the full context of Knuth's famous phrase statement:
Structured Programming with go to Statements, starting on page 268.
That is, the context of this is talking about optimization based on guessing what will create an efficiency (which Knuth calls "premature") in comparison to optimization based on measurement. In that sense, the type of optimization that OP is performing would not be "premature" in Knuth's setting, as it was driven by a measured evaluation of how much memory was being used by the code.
Further, Knuth is arguing that "small efficiencies" actually do matter, and that any serious engineering discipline would certainly consider a 12% improvement (which was the case he was discussing) to be a significant one... and OP's change is a 75% reduction in memory utilization.
4
u/Resident-Letter3485 Jun 19 '26
Premature optimization is like eating a scoop of ice cream every day instead of having a big bowl of it at the end of the month.