Questions tagged [optimization]

Optimization is the act of improving a method or design. In programming, optimization usually takes the form of increasing the speed of an algorithm, or reducing the resources it requires. Another meaning of optimization is numerical optimization algorithms used in machine learning.

In computer science, program or software optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less memory storage or other resources, or draw less powerWikipedia. Other resources may include disk access, communication bandwidth, video performance and user interface responsiveness.

Common software-related optimization goals are:

  • Design or algorithm efficiency.
  • Source code level. For example Duff's Device.
  • Build level or optimizer flags, often trading build time for run-time efficiency.
  • Compile level; choosing the best compiler.
  • Assembly level. The best machine mapping to a problem.
  • Run time. Examples include virtual machine parameters and profile guided optimization.

Less strictly software-related optimization goals are:

  • Query Optimization. This is the process of improving the design of a database query to increase performance. Use the tag for questions about query optimization.
  • Numerical Optimization. Use the tag for questions about numerical optimization.
  • Teacher/colleague happiness. Writing software in a way that some important person or a group of persons appreciates the code as read by a human, e.g. for clarity and ease of maintenance.

Performance optimization often increases program complexity and reduces its maintainability. Reducing space requirements often means code running slower, while making code run faster often increases its memory demands, although the most profound algorithmic optimization will often improve on both aspects.

A commonly cited peril is "premature optimization" ("the root of all evil", as the saying goes). In contrast, the most dramatic effect on optimization is at the Design Level via algorithm efficiency; this is the earliest stage of development thus seemingly a paradox.

The way to resolve this paradox is to consider the correct order of implementations. Correctness should come first (thus starting with the simplest, most self-evidently correct code), algorithmic improvements second, performance-improving "micro-optimizations" come last. "Premature optimization" means focusing on micro-optimizations right away before even formulating a correct solution (which often leads to altogether wrong, though efficiently so, code); or before considering algorithmic optimizations which, if possible, would obviate those micro-optimizations in the first place.

When using this tag, i.e. asking about optimization, please state the optimization goal you have in mind. The lists above illustrate that many possible optimization goals are mutually exclusive (e.g. performance vs simplicity / maintainability). It is hence not possible to optimize for everything; and optimizing without a clear goal is another common peril and makes a question too broad or unclear.

See also:

38871 questions
14
votes
2 answers

Why is my Strassen's Matrix Multiplication slow?

I wrote two Matrix Multiplications programs in C++: Regular MM (source), and Strassen's MM (source), both of which operate on square matrices of sizes 2^k x 2^k(in other words, square matrices of even size). Results are just terrible. For 1024 x…
newprint
  • 6,936
  • 13
  • 67
  • 109
14
votes
5 answers

Can I use more heap than 32 GB with compressed oops?

I could understand that, with compressed oops, we can only use 32 GB of RAM. Is there some way I can use more than that, like by allocating 2 heaps or something?
Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77
14
votes
1 answer

How unpacking strict fields goes together with polymorphism?

The {-# UNPACK #-} pragma tells the compiler to eliminate redundant constructors. Quoting Haskell wiki: For example, given this: data T = T {-# UNPACK #-} !(Int,Float) GHC will represent the type T like this: data T = T Int Float eliminating the…
Petr
  • 62,528
  • 13
  • 153
  • 317
14
votes
11 answers

Which is better/more efficient: check for bad values or catch Exceptions in Java

Which is more efficient in Java: to check for bad values to prevent exceptions or let the exceptions happen and catch them? Here are two blocks of sample code to illustrate this difference: void doSomething(type value1) { ResultType result =…
lilbyrdie
  • 1,887
  • 2
  • 20
  • 24
14
votes
2 answers

Does setting the platform when compiling a c# application make any difference?

In VS2012 (and previous versions...), you can specify the target platform when building a project. My understanding, though, is that C# gets "compiled" to CIL and is then JIT compiled when running on the host system. Does this mean that the only…
AJ.
  • 1,621
  • 1
  • 10
  • 23
14
votes
1 answer

Reliable information about x86 string instruction performance?

Common widsom is that rep movsb is much slower than rep movsd (or on 64-bit, rep movsq) when performing identical operations. However, I've been testing on a few modern machines, and the run times are coming out identical (up to measurement noise)…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
14
votes
7 answers

How to compare performance of two pieces of codes

I have a friendly competition with couple of guys in the field of programming and recently we have become so interested in writing efficient code. Our challenge was to try to optimize the code (in sense of cpu time and complexity) at any cost…
Pouya
  • 1,266
  • 3
  • 18
  • 44
14
votes
2 answers

CS5 Hiding layers is painfully slow

Is it only me that thinks the CS5 scripts runs painfully slow? These few lines takes over 1 minute to execute. for (n=0; n
Max Kielland
  • 5,627
  • 9
  • 60
  • 95
14
votes
2 answers

Why explicitly state "inline" in C++

Possible Duplicate: When should I write the keyword 'inline' for a function/method? So this is a question that has bugged me for a while and I can't get a definitive answer. My understanding is that a good compiler will generally realise when it…
dmon
  • 1,344
  • 1
  • 14
  • 29
14
votes
4 answers

Multiple ParticleSystems in cocos2d

I wonder about what road I should go with ParticleSystem. In this particular case I want to create 1-20 small explosions at the same time but with different positions. Right now I'm creating a new ParticleSystem for each explosion and then release…
Mattias Akerman
  • 363
  • 4
  • 14
14
votes
1 answer

Branch prediction at php

Just read a great post about branch prediction. I was trying to reproduce it using php language.
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81
14
votes
5 answers

re implement modulo using bit shifts?

I'm writing some code for a very limited system where the mod operator is very slow. In my code a modulo needs to be used about 180 times per second and I figured that removing it as much as possible would significantly increase the speed of my…
PgrAm
  • 671
  • 2
  • 7
  • 19