6 ms·
I'm glad to see something like this. People often take the typical optimization mantra too far. The minute you mention optimization on most internet forums (esp
by zamalek 12y ago
I'm glad to see something like this. People often take the typical optimization mantra too far. The minute you mention optimization on most internet forums (especially StackOverflow) you get slammed for promoting "premature optimization."
That's not to say premature optimization doesn't exist. Sometimes writing faster code is the difference between using a hash instead of a list. Sometimes it's spending an entire day optimizing a function that's called once. The former is performance aware, the later is premature. It seems as though most would have you believe that they both fall into the "premature" bucket.
It's as though people are using it as an excuse for their personal ignorance and would rather continue to revel in this ignorance than expand what they know. As far as I am concerned these "unimportant tidbits" about performance are gifts that keep giving.
/somewhat offtopic rant
- Retric 12y agoThere are few absolutes in coding. Some times optimizing a function that's called once is worth it. People often perceive software that takes a long time to load is slow even if everything else is fast. Though even just changing the loading screen can do a lot even if the actual time something takes to load does not change. PS: If all someone wants to do is rotate an image loading photoshop really is slow.
- grokys 12y agoYes, but it's unlikely that someone asking "is it faster to concatenate strings in C# using the + operator or StringBuilder" on StackOverflow is someone that is writing a low-level database engine like SQLite. In my (totally anecdotal) experience the people who are more concerned about performance are beginners, because that is something that they can grasp, and this is where the "don't prematurely optimize" people are coming from. Obviously someone who is writing a low-level database engine knows that performance matters, and likewise, is likely to be running benchmarks rather than asking vague questions on SO.
- zamalek 12y agoIndeed. However, keep in mind that as this beginner "ramps up" to the knowledge level of someone who is working on a SQL database he will need a source to find out where costs lie. He's done some profiling, finds an expensive method with massive amounts of string concats, heads to Google and all he can find is "don't prematurely optimize." You can supplement an answer with "don't prematurely optimize," but the presumption that premature optimization is occurring is a premature assumption. He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever. ~ Chinese Proverb I would work with beginners that are not fools, than experts who are.
- marcosdumay 12y agoNope, if he got to that level, he'll know that the only way to really know it is testing. He may still ask why one is slower than the other (if it is, I don't know that), but that's a completely different question that in most contexts will get a nice answer.
- bunderbunder 12y agoSuppose, for a moment, that our hero is seeing this situation for the first time and doesn't know what the alternatives are. Tests that don't cover the best option can't possibly identify the best option. So if you want to do a really good test, going somewhere to ask, "What else should I try?" is hardly an asinine course of action. On the contrary, if our hero is new to this stuff, or is new to the platform in question, or anything like that, then asking others for advice should probably qualify as due diligence.
- sosborn 12y ago> He's done some profiling In my experience on Stack Overflow, the people getting hit with accusations of "premature optimization" have not gone this far.
- bunderbunder 12y agoAnd that's decent advice in response to the "is it faster to concatenate strings in C# using the + operator or StringBuilder" questions. But I've also asked Stack Overflow questions that drowned under a tsunami of premature optimization tut-tutting from people who I suspect didn't even fully understand the question, and then sighed and proceeded to take a couple days to figure it out myself and improve application performance in a critical section by 50%. Which is fine, but once upon a time I thought Stack Overflow might be a resource where people might occasionally save me a bunch of time by saying, "Hey, don't waste your time, I've been there and it's a blind alley." I think part of what's going on there is that SO's gamification can create some kind of perverse incentives for answerers. If you want to get the upvotes, you have two good courses of action: You can either already have 5 digits' worth of karma, or you can answer first. The first option isn't available to the vast majority of SO users, so most go with the latter one. This drives down the quality of answers. It also creates perverse incentives for behaviors such as users getting in a quick low-quality response that's just good enough to get a few upvotes from people who already understand what you're talking about, and then (maybe) following it up with an edit that provides a high-quality response that would be useful to the asker.
- zamalek 12y agoIt helps to preempt the typical response. If I post a question that's "premature optimization bait" I end it with: > I'm optimizing this due to profiler results. You'd be surprised how many people still question whether I'm prematurely optimizing in the comments, but the actual answers tend to be more helpful.
- dragonwriter 12y agoHave you tried something more along the lines of: I'm trying to do X. Currently, I'm using Y mechanism, and it produces correct results. However, the performance sucks -- profiling shows me this is a big time sink in my program. So... (actual question.) It may help with kneejerk "premature optimization" if the profiler results are mentioned before the request about how to optimize (at least, it should to the extent that the problem is people stopping reading because they see the optimization request so that they never read the comment about profiler results.)
- nitrogen 12y agoIt's not a completely useless question. I wrote an API data validation gem for Ruby that needs to be as unobtrusive as possible, so it needs to be fast. I found that building strings by interpolation (e.g. "#{str1}#{str2}") is quite a bit faster than addition (e.g. str1 + str2).
- teyfille 12y agoTo be fair, I once fixed a java application that completely crashed due to using string concatenation for data export, rather than a StringBuffer. It does matter sometimes even in workaday applications.
- ownagefool 12y agoThe argument behind premature optimization only really stands when first developing a product. Once you have an established product, it's no longer premature, it's just optimization.
- emodendroket 12y agoWell, only if you never add any new features to it.
- wvenable 12y agoAs long as you are not optimizing those new features as you are adding them, it's not premature optimization.
- Scuds 12y agoWith a full suite of automated regression tests you'll have a great deal of confidence that any changes you've made didn't break anything. Go nuts and experiment!
- equoid 12y agoPremature optimization is that which is done before the need for the optimization is clear. Thus debugged, feature complete (for now) ... 'Established' really has nothing to do with it, if that established product has bugs then those should be addressed before attempting to optimize. Unless of course, the lack of optimization is itself the bug.
- dragonwriter 12y ago> Once you have an established product, it's no longer premature, it's just optimization. Its premature even if the product is completely feature complete and bug-free if the particular optimization being done is being done before more significant optimizations, which, if you aren't using some measurement more sophisticated than "this looks like it could be optimized" to choose what to optimize, it probably is.
- steve-howard 12y agoI'm late to the discussion, but I will say that when you have an established product you have a much better perspective on how the code is organized than when you're still cobbling together V1.0. A firm grasp of the code's organization makes it much easier to contain the optimized part and slap on a /* HERE BE DRAGONS */ header.
- ceejayoz 12y ago> The minute you mention optimization on most internet forums (especially StackOverflow) you get slammed for promoting "premature optimization." Typically, that's because the people asking are asking questions like "which is faster in PHP, print or echo?" to which the answer is generally "there's about a half a second difference if you run it a million times, but you shouldn't be doing that anyways".
- jiggy2011 12y agoSQLite has an advantage because it is a small, well defined program (for a DB) that as is much defined by what it doesn't do as what it does. The typical problem with premature optimisation is that you optimize something that is either subject to having functionality requirements altered in a significant way and the optimisations will make it brittle when you need to do this.
- emodendroket 12y agoThat's because so many people asking are focusing on micro-optimizations without having profiled and while ignoring bigger factors, or else don't have working code yet.
- Shog9 12y agoThe other response you'll see all the time? "Measure it!" Low-level optimization is not a task to be undertaken casually. If you don't know where to look, you can end up just wasting a lot of programmer time for zero real-world benefit. If you don't measure before and after, you may even make things worse... The SQLite folks didn't make these changes with a hope and a prayer; they carefully profiled, changed, and profiled again... And they invested years in building a test suite to ensure that such micro-optimizations don't inadvertently break the logic. The results are impressive, but so was the time and effort invested in achieving them. If you're not willing to be so diligent, "premature optimization" is exactly what you're doing.
- zamalek 12y agoOh yeah, definitely. In some situations performance implications are obvious. E.g. Adding to a list of unique values in a loop? Use a hash, not a list. That's the kind of thing you do up-front, it sounds obvious but you would be amazed at the bad responses you get on the internet for pointing this kind of stuff out. What I guess I'm trying to say is that you can go in a write obviously slow code "which is fine because you shouldn't prematurely optimize," or you can be informed and, with sometimes no extra development time, write something that's at least competent from the get-go. For everything else? Profile, profile, profile, profile! And make sure you are using a competent profiler. I've wasted days because of inferior tools, switched to better ones (cough VTune cough) and had major improvements in mere hours.
- Shog9 12y agoThis is where knowing the full Knuth quote helps... > Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. Another common mistake relates to the phrase "penny wise and pound foolish", which applied to programming helps us identify those scenarios where we might waste time micro-optimizing array searches rather than choosing a hashtable (or even pre-sorting the array). Time spent optimizing your architecture will nearly always offer a bigger payoff than micro-optimizations - but once that's done, Knuth's critical 3% is worth remembering.
- ultramancool 12y agoWell, I think the imporatant part to note is that SQLite has been through basic performance review, it's been heavily tested and used at this point. It is a mature piece of software. So optimizations like this can certainly no longer be considered "premature".
- nodata 12y agoThey already optimised everything else, so this isn't premature.
- TorKlingberg 12y agoA common advice is to focus optimization in the few places where it matters, such as in the middle of tight loops. Sqlite is in an interesting situation in than almost all their code could be called in the middle of somebody's tight loop. The same applies to many libraries, operating systems, VMs, etc, but not to most applications.
- snarfy 12y agoGrace Hopper on inefficiency, etc https://www.youtube.com/watch?v=JEpsKnWZrJ8 https://www.youtube.com/watch?v=JEpsKnWZrJ8
- tieTYT 12y ago> Sometimes writing faster code is the difference between using a hash instead of a list. Sometimes it's spending an entire day optimizing a function that's called once. The former is performance aware, the later is premature. I disagree, it depends on the context. If using your original list is so fast that nobody human can notice the difference, it's a waste of time to turn into a hash, not "performance aware". Actually, whether it's a "waste of time" also depends on the context. It could be worse. It could make your code harder to understand and maintain.
- ahoge 12y ago> "premature optimization." Once a feature works as intended, optimizations aren't premature anymore. You're then in a position where you can profile, measure, and compare.
- craigyk 12y agoPremature optimization is also not a true waste of time (IMO) if it's part of the learning process. Man, did I ever 'waste' a lot of time on optimization when I was first learning to code (in C), but: a) I learned what the low-hanging fruit was, so that I was eventually able to make all my code fairly fast without explicit optimization. b) Man, where some of those early programs I wrote blindingly fast... especially compared to the higher-level python/numpy/scipy stuff I write a lot today. It's also nice to repurpose some of that old code (ie as a library) for new applications since every potential bottleneck has already been ironed out.
- nbevans 12y agoOn a mature project like SQLite, nothing is premature anymore - including optimisations. The internet is full of dumbass developers unfortunately. Both on HN, StackOverflow, and well everywhere.