72
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 ...
- 40.5k
62
votes
Accepted
In software programming, would it be possible to have both CPU and GPU loads at 100%?
Theoretically yes, but practically it's rarely worth it.
Both CPUs and GPUs are turing-complete, so any algorithm which can be calculated by one can also be calculated by the other. The question is ...
- 23k
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 ...
- 101k
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 ...
- 97.8k
39
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 ...
- 130k
38
votes
How does branch prediction work, if you still have to check for the conditions?
Think of it like a road trip without GPS. You come to an intersection, and think you need to turn, but aren't completely sure. So you take the turn, but ask your passenger to check the map. Maybe ...
- 146k
36
votes
Accepted
How to deal with misconceptions about "premature optimization is the root of all evil"?
It seems you are looking for shortcuts not to try out the "purest naive implementation" first, and directly implement a "more sophisticated solution because you know beforehand that the naive ...
- 194k
36
votes
In software programming, would it be possible to have both CPU and GPU loads at 100%?
It is not related to game programming. Some scientific code can also use both the GPU and the CPU.
With careful -and painful- programming, e.g. by using OpenCL or CUDA, you could load both your GPU ...
- 32.2k
27
votes
Accepted
How does branch prediction work, if you still have to check for the conditions?
Of course the condition is checked every single time. You cannot avoid this.
Branch prediction and many other tricks that modern CPUs do are all about achieving as much processing as possible in ...
- 31.9k
26
votes
Accepted
Am I prematurely optimizing?
Without reading anything but the title: Yes.
After reading the text: Yes. Though it is true that maps and shared pointers etc. do not perform well cache-wise, you will most certainly find that what ...
- 468
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 ...
- 130k
21
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 ...
- 16.6k
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 ...
- 198k
20
votes
Accepted
Should you minimize the creation of a lot of small objects?
In general, no, you shouldn't avoid creating objects for fear of performance loss. There are several reasons for this.
Using objects is kind of the point of using Java. Avoiding them preemptively is ...
- 106k
20
votes
What is the meaning of the 90/10 rule of program optimization?
This isn't a law of nature, but a rule of thumb born out by wide experience. It is also known as the 80/20 rule, and is only ever a rough approximation.
Loops, Branches and other flow control.
Each ...
- 10.2k
18
votes
How to deal with misconceptions about "premature optimization is the root of all evil"?
Ask yourself this:
Is the software NOT meeting performance specification?
Does the software HAVE a performance issue?
These are reasons to optimize. So, if people are opposed, just show them the ...
- 10.8k
17
votes
Is it wise to rely on optimizations?
Both :-)
Seriously, premature optimization can be a problem - you might wind up spending hundreds of hours optimizing a routine that is only run once/week. Also, fully optimized code is often harder ...
- 13.8k
17
votes
How important is memory alignment? Does it still matter?
Yes, memory alignment still matters.
Some processors actually can't perform reads on non-aligned addresses. If you're running on such hardware, and you store your integers non-aligned, you're likely ...
- 677
16
votes
Linking two or more different programming languages
Linking code (using the linker, e.g. ld often started by gcc or gfortran compilation commands) written in two different languages is implementation specific. It is often called foreign function ...
- 32.2k
16
votes
What is the meaning of the 90/10 rule of program optimization?
Loops.
I'm tempted to stop there! :-)
Consider this program
1. do_something
2. loop 10 times
3. do_another_thing
4. loop 5 times
5. do_more_stuff
Line 1 is executed once whilst ...
- 754
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 ...
- 45.6k
14
votes
Accepted
How important is memory alignment? Does it still matter?
Yes both alignment and arrangement of your data can make a big difference in performance, not just a few percent but few to many hundreds of a percent.
Take this loop, two instructions matter if you ...
- 969
13
votes
performance versus reusability
You should do whatever yields the greater business value in this situation.
Writing software is always a trade-off. Almost never are all valid goals (maintainability, performance, clarity, ...
- 106k
13
votes
Accepted
Why is Haskell unable to avoid repeated evaluation without the monomorphism restriction?
It's the same function name, with the same arguments, but potentially different return types and implementations, because it's polymorphic. That means if it's called in a context expecting an (Int, ...
- 146k
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 ...
- 130k
13
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.
...
- 130k
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, ...
- 704
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
optimization × 388algorithms × 62
performance × 55
c++ × 47
compiler × 34
design × 21
c# × 20
c × 20
java × 17
database × 16
php × 12
architecture × 11
javascript × 11
python × 11
web-development × 11
design-patterns × 10
programming-practices × 9
math × 9
profiling × 9
caching × 8
cpu × 8
memory-usage × 8
compilation × 8
object-oriented × 7
sql × 7