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
13
votes
4 answers

Does compiler optimize operation on const variable and literal const number?

Let's say I have class with field: const double magicalConstant = 43; This is somewhere in code: double random = GetRandom(); double unicornAge = random * magicalConstant * 2.0; Will compiler optimize my code so that it doesn't calculate…
Hooch
  • 28,817
  • 29
  • 102
  • 161
13
votes
4 answers

How to index a date column with null values?

How should I index a date column when some rows has null values? We have to select rows between a date range and rows with null dates. We use Oracle 9.2 and higher. Options I found Using a bitmap index on the date column Using an index on date…
Heinz Z.
  • 1,537
  • 3
  • 14
  • 29
13
votes
1 answer

Genymotion won't stop optimizing app

I'm trying to use Genymotion with Android Studio so that I can test an app, but whenever I try to launch the emulator it says "Android is starting... Optimizing app [#] of 74" and it optimizes for a while. Eventually it stops optimizing, but then it…
ssaarraahhld
  • 165
  • 2
  • 7
13
votes
7 answers

Python tips for memory optimization

I need to optimize the RAM usage of my application. PLEASE spare me the lectures telling me I shouldn't care about memory when coding Python. I have a memory problem because I use very large default-dictionaries (yes, I also want to be fast). My…
Tal Weiss
  • 8,889
  • 8
  • 54
  • 62
13
votes
4 answers

What is this asm style "x | 0" some javascript programmers are now using?

I've seen some performance critical javascript code, like the one on this project that makes extensive use of bitwise OR operations with 0. Ex: GameBoyAdvanceCPU.prototype.write8 = function (address, data) { address = address | 0; data = data |…
13
votes
4 answers

Is there any workaround to "reserve" a cache fraction?

Assume I have to write a C or C++ computational intensive function that has 2 arrays as input and one array as output. If the computation uses the 2 input arrays more often than it updates the output array, I'll end up in a situation where the…
VAndrei
  • 5,420
  • 18
  • 43
13
votes
4 answers

Finding the minimal coverage of an interval with subintervals

Suppose I have an interval (a,b), and a number of subintervals {(ai,bi)}i whose union is all of (a,b). Is there an efficient way to choose a minimal-cardinality subset of these subintervals which still covers (a,b)?
Alex Coventry
  • 68,681
  • 4
  • 36
  • 40
13
votes
3 answers

Can I empty-base optimize mutable data?

I have a class template which looked like this: template class my_class { public: T f() const { resource_lock a_lock(some_mutex); return some_policy.some_operation(some_data); } private: …
sbi
  • 219,715
  • 46
  • 258
  • 445
13
votes
8 answers

Initializing a C++ vector to random values... fast

Hey, id like to make this as fast as possible because it gets called A LOT in a program i'm writing, so is there any faster way to initialize a C++ vector to random values than: double range;//set to the range of a particular function i want to…
Flamewires
  • 380
  • 1
  • 4
  • 15
13
votes
1 answer

Chrome Timeline buffer usage

I'm trying to optimize the animations of а sitе. I'm using Chrome dev tools(network/timeline/profile) to get some stats and have found out that when I track the timeline, the usage always buffers to 100% in very short time. I make a test on…
13
votes
2 answers

learning sample of likely() and unlikely() compiler hints

How can I demonstrate for students the usability of likely and unlikely compiler hints (__builtin_expect)? Can you write an sample code, which will be several times faster with these hints comparing the code without hints.
osgx
  • 90,338
  • 53
  • 357
  • 513
13
votes
1 answer

Maximize profit in scheduling unit tasks with dependencies

Problem I have n jobs to schedule in P seconds on unlimited number of machines with dependencies between the jobs i.e . for every job there is a set of jobs which are to be scheduled only after this job is finished. The profit for scheduling the ith…
v78
  • 2,803
  • 21
  • 44
13
votes
1 answer

Does order of constructors / cases / guards / if-then-else matter to performance?

I saw this comment in Containers/Data/Set/Base.hs -- [Note: Order of constructors] -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- The order of constructors of Set matters when considering performance. -- Currently in GHC 7.0, when type has 2 constructors, a…
Michael Fox
  • 3,632
  • 1
  • 17
  • 27
13
votes
1 answer

How to implement a generic neural network efficiently in Haskell?

A neural network is actually just a huge function with many parameters, so you might think that it would be beautiful to write such a function in a functional language, but having worked on some NN libraries for other languages, I have certain…
Cristian Garcia
  • 9,630
  • 6
  • 54
  • 75
13
votes
3 answers

Produce loops without cmp instruction in GCC

I have a number of tight loops I'm trying to optimize with GCC and intrinsics. Consider for example the following function. void triad(float *x, float *y, float *z, const int n) { float k = 3.14159f; int i; __m256 k4 =…
Z boson
  • 32,619
  • 11
  • 123
  • 226
1 2 3
99
100