Skip to main content
76 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 ...
gnasher729's user avatar
  • 46.7k
75 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 ...
BenM's user avatar
  • 921
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 ...
Jörg W Mittag's 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 ...
candied_orange's user avatar
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 ...
Christophe's user avatar
  • 79.9k
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 ...
amon's user avatar
  • 135k
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 ...
pjc50's user avatar
  • 14.6k
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 ...
steffen's user avatar
  • 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 ...
amon's user avatar
  • 135k
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 ...
rwong's user avatar
  • 16.9k
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 ...
Robert Harvey's user avatar
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 ...
Kilian Foth's 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 ...
Caleth's user avatar
  • 11.5k
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 { ...
Phill  W.'s user avatar
  • 13k
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 ...
Matthew Walton's 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 ...
Nick Keighley's user avatar
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 ...
Eric Lippert's user avatar
  • 46.3k
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 ...
old_timer's user avatar
  • 969
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. ...
amon's user avatar
  • 135k
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 ...
amon's user avatar
  • 135k
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, ...
Baldrickk's user avatar
  • 724
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 ...
Erik Eidt's user avatar
  • 34.3k
12 votes

Why is it is easier to reason about programming languages and programs that have no side effects?

Obviously, you can find examples of incredibly difficult to read pure functions that perform the same calculations as functions with side effects that are much easier to read. Especially when you use ...
Karl Bielefeldt's user avatar
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 ...
candied_orange's user avatar
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 ...
David Arno's user avatar
  • 39.4k
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 ...
Hans-Martin Mosner's user avatar
12 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 ...
Christophe's user avatar
  • 79.9k
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 ...
amon's user avatar
  • 135k
11 votes

Why is it is easier to reason about programming languages and programs that have no side effects?

An interesting property of languages without side-effects is that introducing parallelism, concurrency, or asynchrony cannot change the meaning of the program. It can make it faster. Or it can make it ...
Jörg W Mittag's user avatar

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