Premature optimization is the root of all evil (or at least most of it) in programming.
-Donald Knuth
The road to programming hell is paved with “best practices” applied too early. Very few abstractions, extractions, or architectures are inherently bad (although I’ll contend that a few are). But most of them are downright evil if applied before they’re called for.
-David Heinemeier Hansson, Winning is the worst thing that can happen in Vegas
Most of the time, if you're thinking about big-O, you're practicing pre-mature optimization.
— Shit HN Says (@shit_hn_says) February 11, 2013
Press down or up to navigate.
StringBuffer/StringBuilder
If you do this in a long loop or a function that calls a lot of different fuctions only than it makes sense
SomeObject x = null;
for(int i = 0; i<1000;i++){
x = someMethod();
...
}
List xList = new ArrayList(1000);
for(int i = 0; i<1000;i++){
xList.add(...);
...
}
List xList = ...;
for(int i = 0; i<1000;i++){
final SomeObject o = xList.get(i);
...
}
A common misconception is that optimized code is necessarily more complicated, and that therefore optimization always represents a trade-off. However, in practice, better factored code often runs faster and uses less memory as well. In this regard, optimization is closely related to refactoring, since in both cases we are paying into the code so that we may draw back out again later if we need to. We don't (yet) have PrematureRefactoring? regarded as CategoryEvil.
C2, Premature Optimization
-- Guillermo Schwarz
note that faster is not always better