Questions tagged [optimization]

Optimization is the process of improving an existing program to make it work more efficiently or/and using less resources.

Since optimization may occur at various stages (analysis, design, implementation, compiler, runtime, etc.) during the development of a program, and according to the FAQ, the following types of questions are certainly valid:

  • What is/is it considered optimization, with a detailed question about what you are referring to (remember that "programmers" is not "code review"). Theoretical questions on algorithms efficiency are OK.
  • What can(not) be optimized given a context (for example #1 or #2).

Beware that some more "general" questions about optimization have already been asked. Most notably "when to perform optimization", "when to stop optimizing", "is premature optimization really bad"...

More information on optimization may be found on Wikipedia: Optimization.

392 questions
291
votes
17 answers

Is premature optimization really the root of all evil?

A colleague of mine today committed a class called ThreadLocalFormat, which basically moved instances of Java Format classes into a thread local, since they are not thread safe and "relatively expensive" to create. I wrote a quick test and…
Craig Day
  • 563
  • 3
  • 5
  • 5
197
votes
32 answers

Is micro-optimisation important when coding?

I recently asked a question on Stack Overflow to find out why isset() was faster than strlen() in PHP. This raised questions around the importance of readable code and whether performance improvements of micro-seconds in code were worth even…
Boz
  • 171
  • 2
  • 3
  • 10
82
votes
14 answers

When is optimization not premature and therefore not evil?

"Premature optimization is root of all evil" is something almost all of us have heard/read. What I am curious what kind of optimization not premature, i.e. at every stage of software development (high level design, detailed design, high level…
Gaurav
  • 3,729
  • 2
  • 25
  • 43
80
votes
20 answers

Is it ok to replace optimized code with readable code?

Sometimes you run into a situation where you have to extend/improve some existing code. You see that the old code is very lean, but it's also difficult to extend, and takes time to read. Is it a good idea to replace it with modern code? Some time…
Coder
  • 6,958
  • 5
  • 37
  • 49
72
votes
10 answers

Clean readable code vs fast hard to read code. When to cross the line?

When I write code I always try to make my code as clean and readable as possible. Every now and then there comes a time when you need to cross the line and go from nice clean code to slightly uglier code to make it faster. When is it OK to cross…
72
votes
19 answers

Why should I care about micro performance and efficency?

Many questions and answers on the C/C++ pages, specifically or indirectly discuss micro performance issues (such is the overhead of an indirect vs direct vs inline function), or using an O(N2) vs O(NlogN) algorithm on a 100 item list. I always code…
mattnz
  • 21,315
  • 5
  • 54
  • 83
68
votes
9 answers

What is the meaning of the 90/10 rule of program optimization?

According to Wikipedia, the 90 / 10 rule of program optimization states that “90% of a program execution time is spent in executing 10% of the code” (see the second paragraph here). I really don't understand this. What exactly does this mean? How…
Rakshith Ravi
  • 765
  • 1
  • 5
  • 15
44
votes
15 answers

How have languages influenced CPU design?

We are often told that the hardware doesn't care what language a program is written in as it only sees the compiled binary code, however this is not the whole truth. For example, consider the humble Z80; its extensions to the 8080 instruction set…
Gaius
  • 646
  • 1
  • 7
  • 17
43
votes
7 answers

In software programming, would it be possible to have both CPU and GPU loads at 100%?

This is a general question on a subject I've found interesting as a gamer: CPU/GPU bottlenecks and programming. If I'm not mistaken, I've come to understand that both CPU and GPU calculate stuff, but that one is better in some calculations than the…
Azami
  • 549
  • 4
  • 8
41
votes
16 answers

Should you sacrifice code readability with how efficient code is?

Should you sacrifice code readability with how efficient code is? e.g. 3 lines of code into 1 line. I read in Code Craft by Pete Goodliffe that readability is key. Your thoughts?
TeaDrinkingGeek
  • 1,519
  • 3
  • 13
  • 28
41
votes
5 answers

JIT compiler for C, C++, and the likes

Is there any just-in-time compiler out there for compiled languages, such as C and C++? (The first names that come to mind are Clang and LLVM! But I don't think they currently support it.) Explanation: I think the software could benefit from runtime…
Ebrahim Mohammadi
  • 549
  • 1
  • 4
  • 7
41
votes
6 answers

How does branch prediction work, if you still have to check for the conditions?

I was reading the popular answer about Branch Prediction from https://stackoverflow.com/q/11227809/555690, and there is something confusing me: If you guessed right, it continues on. If you guessed wrong, the captain will stop, back up, and yell…
Saturn
  • 3,887
  • 9
  • 30
  • 40
39
votes
4 answers

How fast can Go go?

Go is one of the few languages that are supposed to run 'close to the metal', i. e. it's compiled, statically typed and executes code natively, without a VM. This should give it a speed advantage over Java, C# and the like. It seems, however, that…
Greg Slodkowicz
  • 971
  • 2
  • 9
  • 11
31
votes
2 answers

Implementation of pure abstract classes and interfaces

Although this isn't mandatory in the C++ standard, it seems the way GCC for example, implements parent classes, including pure abstract ones, is by including a pointer to the v-table for that abstract class in every instantiation of the class in…
Clinton
  • 1,053
  • 1
  • 11
  • 14
31
votes
5 answers

Is passing arguments as const references premature optimization?

"Premature optimization is the root of all evil" I think this we can all agree upon. And I try very hard to avoid doing that. But recently I have been wondering about the practice of passing parameters by const Reference instead of by Value. I have…
CharonX
  • 1,633
  • 1
  • 11
  • 23
1
2 3
26 27