TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Basic Micro-Optimizations

33 pointsby Ashuuover 10 years ago

6 comments

robert_tweedover 10 years ago
I like the basic premise, which in effect is just &quot;write good code&quot; and, if all else is equal, write efficient code.<p>Premature optimisation is a problem only when you spend too long on it, or it reduces readability, robustness, testability, etc. If it&#x27;s equally easy to write but better, why not? There&#x27;s no excuse for routinely writing bad code and &quot;premature optimisation is the root of all evil&quot; is all-too often used as an excuse for sloppiness.<p>However, I <i>possibly</i> disagree with the string formatting example. If it&#x27;s a function that gets called a lot and does a special-case string conversion like this, fine go ahead and optimise it. But that&#x27;s a <i>real</i> optimisation, not what the author has termed a micro-optimisation, which I take to be something you should just do routinely (like ++i instead of i++ in C++).<p>If you have lots of string conversions throughout your code then the chances are most of them are going to be sprintfs or whatever is the most flexible tool in the language you are using. In these cases, you should just stick with what is idiomatic within the context of that project. It makes <i>reading</i> the code later a lot faster when everything is similar. It also tends to make it easier to change when your simple special cases need to become more complicated.<p>For example, I have in previous projects standardised on regular expressions for almost all string comparisons, even in situations where a simple substring compare would be much more efficient. However, since 90% of the codebase is using regular expressions to do complex comparisons, it just makes life easier if they are used everywhere unless there&#x27;s a really, really good reason to do things differently. It reduces cognitive load when reading the code if it follows a similar style throughout.<p>It also makes maintenance easier when you use the most flexible tool at your disposal everywhere instead of special-casing. Let&#x27;s say you expect the first character of your string to be an &quot;a&quot; and you do it with substr(foo,1)==&quot;a&quot;. Later, you need to make it case-insensitive because of a bug. With regex you just add an &quot;i&quot; flag, but with the special case, you need a tolowercase call. No biggie, but the next day you need to support unicode accents. Uh oh...<p>If you have a large codebase where string processing is all done in the same way, when you get a bug like not recognising &quot;á&quot; as &quot;a&quot; then at least you will find that all parts of your system behave consistently. Fixing the problem should require roughly the same fix everywhere. The test cases can all be the same. Going back to the author&#x27;s example, there&#x27;s no guarantee that sprintf(&quot;%d&quot;,x) and itoa(x) will produce the same output on all platforms, so it&#x27;s possible this change although it should be functionally identical, might in reality introduce new edge-cases that you need to test for.<p>If you&#x27;ve got special cases everywhere then you&#x27;re going to get different sorts of bugs in different parts of your system which can lead to issues being much harder to trace, harder to test and harder to fix.<p>TL;DR: Optimise for readability first. Then optimise for performance. Allocate the time you have wisely. Homogeneity is reasonable substitute for DRY; special-cases for common patterns are usually bad and can introduce bugs.
deedubayaover 10 years ago
I&#x27;m all for these types of optimizations, so long as they don&#x27;t come with the price of readability. The more complex an optimization is, the more likely it&#x27;ll cost more in the long run (developer time) than save (hardware resources).<p>Hardware&#x2F;Memory&#x2F;CPU&#x27;s are still much cheaper than developer time.
评论 #8683217 未加载
评论 #8683009 未加载
jayvanguardover 10 years ago
I like the term micro-optimization. I wouldn&#x27;t use them regularly in my code, maybe just in tight loop or something, but they aren&#x27;t too invasive and can actually flag the code for future maintainers.<p>In fact, I&#x27;d consider adding a little &quot;&#x2F;&#x2F; micro-optmized&quot; comment at the end of one of these to communicate to future developers that this mildly odd line is a useful but non-essential little optimization. So feel free to change it but you&#x27;ll probably want to read up a little or do a quick benchmark before you do that.
评论 #8682546 未加载
评论 #8682477 未加载
chatmastaover 10 years ago
In regards to growing array one-by-one, obviously be very careful with that. In C, issues to look out for are malloc overhead and memory alignment issues if you are not realloc&#x27;ing in even sizes. Also see Mozilla&#x27;s recent post on this issue. [0]<p>[0] <a href="https://blog.mozilla.org/nnethercote/2014/11/04/please-grow-your-buffers-exponentially/" rel="nofollow">https:&#x2F;&#x2F;blog.mozilla.org&#x2F;nnethercote&#x2F;2014&#x2F;11&#x2F;04&#x2F;please-grow-...</a>
评论 #8683215 未加载
vkjvover 10 years ago
The author is just giving what I would call, &quot;optimizations&quot; or maybe more plainly, &quot;good code&quot;.<p>To me, the term, &quot;premature optimization&quot; means optimizing something before measuring that your optimization has any effect.
oweilerover 10 years ago
Premature optimisation is... ah, forget it ;).
评论 #8682671 未加载