Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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
  • 45.7k
74 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
  • 911
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 ...
Philipp's user avatar
  • 23.4k
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
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 ...
amon's user avatar
  • 134k
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 ...
Basile Starynkevitch's user avatar
35 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 ...
Doc Brown's user avatar
  • 209k
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
  • 13.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
  • 134k
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.3k
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
  • 12.3k
17 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 ...
Jon Raynor's user avatar
  • 11.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 ...
Dan Pichelman's user avatar
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.2k
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
  • 134k
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
  • 134k
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
  • 714
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.1k
13 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

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

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