88 votes
Accepted

Does C++ compiler remove/optimize useless parentheses?

The compiler does not actually ever insert or remove parentheses; it just creates a parse tree (in which no parentheses are present) corresponding to your expression, and in doing so it must respect ...
user avatar
69 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 ...
user avatar
  • 37.4k
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 ...
user avatar
  • 22.6k
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 ...
user avatar
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 ...
user avatar
46 votes

Does C++ compiler remove/optimize useless parentheses?

The parentheses are there solely for your benefit - not the compilers. The compiler will create the correct machine code to represent your statement. FYI, the compiler is clever enough to optimise it ...
user avatar
  • 48k
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 ...
user avatar
  • 123k
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 ...
user avatar
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 ...
user avatar
  • 184k
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 ...
user avatar
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: ...
user avatar
  • 10.3k
27 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 ...
user avatar
  • 123k
26 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 ...
user avatar
  • 31.4k
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 ...
user avatar
  • 458
23 votes

Does C++ compiler remove/optimize useless parentheses?

The answer to the question you actually asked is no, but the answer to the question you meant to ask is yes. Adding parentheses does not slow down the code. You asked a question about optimisation, ...
user avatar
  • 8,075
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 ...
user avatar
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 ...
user avatar
  • 9,784
20 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 ...
user avatar
  • 16.4k
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 ...
user avatar
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 ...
user avatar
  • 10.6k
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
15 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 ...
user avatar
  • 45.2k
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 ...
user avatar
  • 7,822

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