77
votes
Accepted
Is passing arguments as const references premature optimization?
"Premature optimisation" is not about using optimisations early. It is about optimising before the problem is understood, before the runtime is understood, and often making code less readable and less ...
76
votes
Shouldn't deep copy be the default, not shallow copy?
Let's say we have a Car that has four Wheels and an Owner. If we want to deep-copy the Car, we'd probably want to copy the Wheels, but it probably wouldn't make much sense to copy the Owner (and thus ...
54
votes
Do compilers optimise in concurrency?
Asuming expensive_calc_one and expensive_calc_two are pure functions
Unfortunately, determining whether a function is pure is equivalent to solving the Halting Problem in the general case. So, you ...
53
votes
When is it better to optimize a software for better performance, at the beginning or at the end of the development?
The number one thing should always and forever be readability. If it's slow but readable, I can fix it. If it's broken but readable, I can fix it. If it's unreadable, I have to ask someone else what ...
48
votes
Accepted
Better solutions than joining table for Many to Many?
Quote of the day:
Premature optimization is the root of all evil- C.A.R. Hoare
There is no reason to assume that the join will be slow, if you made sure that the join table is indexed on its both ...
40
votes
Accepted
Implementation of pure abstract classes and interfaces
In C# and Java implementations, the objects typically have a single pointer to its class. This is possible because they are single-inheritance languages. The class structure then contains the vtable ...
32
votes
Shouldn't deep copy be the default, not shallow copy?
Without keeping a big table of all the objects you've copied so far, you can't safely deep copy objects at all if there may be circular references.
where every object always has a copy method
Is ...
26
votes
When is it better to optimize a software for better performance, at the beginning or at the end of the development?
If a certain level of performance is necessary (a non-functional requirement), then that should be a design goal from the start. E.g. this can influence which technologies might be appropriate, or how ...
24
votes
Accepted
Does memoization skew benchmarks?
This sounds like you are indecisive about the overall goal of your benchmark.
Do you want to measure the speed in "production"? The speed of the program when run under "real-world ...
22
votes
Is passing arguments as const references premature optimization?
TL;DR: Pass by const reference is still a good idea in C++, all things considered. Not a premature optimization.
TL;DR2: Most adages don't make sense, until they do.
Aim
This answer just tries to ...
22
votes
How do I find what's causing a task to be slow, when CPU, memory, disk and network are not used at 100%?
When dealing with a process that takes too much time, the tool you need from your toolbox is profiling: find out how much time is spent in each portion of the process.
As we are are talking about long ...
21
votes
Accepted
Relevance of optimization techniques
Performance optimization doesn't lend itself to these kinds of generalized rules, and I'm not sure that the rules you proposed were ever good ways to optimize.
Here's a better plan:
Set specific ...
19
votes
Better solutions than joining table for Many to Many?
I could query the joining table for this information but I imagine queries would be slow since there's no clear way to horizontally partition the data.
Assuming the Primary key on this table is { ...
16
votes
When is it better to optimize a software for better performance, at the beginning or at the end of the development?
when would be the best time to optimize a software for better performance(speed).
Begin by removing from your mind the concept that performance is the same thing as speed. Performance is what the ...
14
votes
How can arithmetic, like a bit shift, avoid branching?
This kind of microoptimization is usually avoided because it hurts code readability – and microoptimizations is the job of the compiler. But sometimes these techniques can be legitimately useful.
...
13
votes
Accepted
Redundant code sent down the pipe with Micro-frontends
You're absolutely correct that there's a tradeoff involved here: you are trading in some aspects of the user experience to get a better developer experience (which in turn might improve the user ...
13
votes
Accepted
Avoid if-else block in favor of default assignment followed by if
This is more of a style issue than anything else, and very susceptible to personal opinion.
The second example in the question more clearly shows intent, which increases the readability of the code, ...
13
votes
Accepted
Memory optimization of public methods in java
private methods can never be overridden, whereas protected and public methods can be overridden.
As a consequence of this, the underlying runtime knows that for private methods:
There is no need to ...
13
votes
Is there a way to speed up a big switch statement?
The switch instruction is a good choice here: the optimizer generally makes use of a jump table or an indirect branch. It’s difficult to outperform it with other constructs (even on older cpus).
An ...
12
votes
Accepted
C++ Dependency Injection vs Memory Usage
Well you're right about something being wrong. But I highly doubt worrying about memory usage is going to fix it.
Unless you can point to some real world data that shows you have a memory problem at ...
12
votes
Global state variables vs constructor parameter passing
Performance-wise only please.
You have already made the wrong decision, right there. This is called "premature optimisation" and it's frowned upon, for good reasons.
The correct question to be asking ...
12
votes
Is passing arguments as const references premature optimization?
In DonaldKnuth's paper "StructuredProgrammingWithGoToStatements", he wrote: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts ...
12
votes
Is there a way to speed up a big switch statement?
For an architecture such as this, a switch statement is actually pretty efficient. If your target speed is 1.78 MHz (sounds like some Z80 machine, the TRS-80 model II had this clock speed) and your ...
12
votes
Is it better to iterate over data once and do multiple complex operations, or to iterate multiple times with simpler operations?
Optimize for clarity. Iteration overhead tends to be low.
Some people think that this kind of code
for x in data:
a(x)
b(x)
is going to be much more efficient than
for x in data:
a(x)
for x in ...
11
votes
Is passing arguments as const references premature optimization?
Passing by ([const][rvalue]reference)|(value) should be about the intent and promises made by the interface. It has nothing to do with performance.
Richy's Rule of Thumb:
void foo(X x); // ...
11
votes
Is using 64 bit integers (long long) faster than less bits ones?
Robert Harvey's answer is fully correct, but since you mentioned Game Engine Architecture, I think it is worth to add a few words to the case where speed actually could matter.
The described effect of ...
11
votes
How to optimize average rating calculation in a review system?
But assuming it's a large-scale application, updating the average each time would not be a good idea, because it would be a significant DB load to fetch all reviews and make average of them each time (...
11
votes
Shouldn't deep copy be the default, not shallow copy?
This is one of those questions that is asked because you are currently thinking of a scenario where it'd be beneficial if you didn't have to explicitly implement it, and you're forgetting a long list ...
10
votes
How to optimize a mixed stack/register bytecode with control flow and side effects?
There's a huge body of research on this topic, so I really recommend reading a compiler book. Muchnik's book can work well as a reference. I really like Modern Compiler Implementation but I think it's ...
10
votes
Accepted
Is using 64 bit integers (long long) faster than less bits ones?
In practice, it's not going to matter most of the time.
It won't matter in most programs.
For some programs where it might potentially matter, it might still not matter because there's no significant, ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
optimization × 407algorithms × 65
performance × 59
c++ × 49
compiler × 36
design × 22
c# × 20
c × 20
java × 18
database × 17
javascript × 12
php × 12
design-patterns × 11
architecture × 11
python × 11
web-development × 11
caching × 10
programming-practices × 9
math × 9
cpu × 9
profiling × 9
object-oriented × 8
memory-usage × 8
compilation × 8
data-structures × 7