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 ...
  • 38.6k
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 ...
  • 22.9k
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 ...
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 ...
  • 126k
37 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 ...
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 ...
  • 188k
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 ...
27 votes
Accepted

Why don't compilers inline everything?

First note that one major effect of inline is that it allows further optimizations to be made at the call site. For your question: there are things which are difficult or even impossible to inline: ...
  • 10.3k
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.5k
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 ...
  • 458
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 ...
  • 126k
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.5k
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 ...
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 ...
  • 10k
19 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 ...
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 ...
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 ...
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 ...
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.4k
15 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 ...
13 votes

Why don't compilers inline everything?

A major limitation is runtime polymorphism. If there is a dynamic dispatch happening when you write foo.bar() then it is impossible to inline the method call. This explains why compilers don't inline ...
  • 7,832
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, ...
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, ...
13 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 ...
  • 959
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 ...
  • 126k

Only top scored, non community-wiki answers of a minimum length are eligible