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
3 answers

Fast(er) algorithm for the Length of the Longest Common Subsequence (LCS)

Problem: Need the Length of the LCS between two strings. The size of the strings is at most 100 characters. The alphabet is the usual DNA one, 4 characters "ACGT". The dynamic approach is not quick enough. My problem is that I am dealing with lot's…
Yiannis
  • 131
  • 1
  • 6
13
votes
1 answer

Warning: 'chart.js'. CommonJS or AMD dependencies can cause optimization bailouts

ng2-charts.js depends on 'chart.js'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
akhilreddy
  • 272
  • 3
  • 6
13
votes
1 answer

Numpy mean of flattened large array slower than mean of mean of all axes

Running Numpy version 1.19.2, I get better performance cumulating the mean of every individual axis of an array than by calculating the mean over an already flattened array. shape = (10000,32,32,3) mat = np.random.random(shape) # Call this Method…
Dan Ganea
  • 540
  • 4
  • 18
13
votes
5 answers

Incrementing: x++ vs x += 1

I've read that many developers use x += 1 instead of x++ for clarity. I understand that x++ can be ambiguous for new developers and that x += 1 is always more clear, but is there any difference in efficiency between the two? Example using for…
beatgammit
  • 19,817
  • 19
  • 86
  • 129
13
votes
3 answers

How do you do eager loading with limits?

In the documentation for eager loading it is stated that: If you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects: class Picture < ActiveRecord::Base has_many…
Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
13
votes
7 answers

Minimize the maximum difference between the heights

Given heights of n towers and a value k. We need to either increase or decrease height of every tower by k (only once) where k > 0. The task is to minimize the difference between the heights of the longest and the shortest tower after modifications,…
Ajay Singh
  • 161
  • 1
  • 1
  • 8
13
votes
4 answers

Helping the compiler to optimize branchy code sequences

I have code sequences in C/C++ that contain lots of branches, something like this: if( condition1 ) return true; if( condition2 ) return true; ... return false; (which is equivalent to return condition1 || condition2 || ...;) Evaluating…
hans
  • 131
  • 2
13
votes
5 answers

How can NSArray be this slow?

I'm coming from a C++/STL world and I wanted to check how objective-c containers are in comparison to stl. I wanted to compare an array of numbers but the only way to add a number to an NSArray is using NSNumber which is utterly slow and drank my…
Daniel
  • 30,896
  • 18
  • 85
  • 139
13
votes
3 answers

Do modern JavaScript JITers need array-length caching in loops?

I find the practice of caching an array's length property inside a for loop quite distasteful. As in, for (var i = 0, l = myArray.length; i < l; ++i) { // ... } In my eyes at least, this hurts readability a lot compared with the straightforward…
Domenic
  • 110,262
  • 41
  • 219
  • 271
13
votes
5 answers

React.memo isn't working - what am I missing?

I'm in the process of refactoring some of our components so I'm trying to incorporate memoization as some components may re-render with the same values (for example, hotlinked image URLs unless they are the same). I have a simple component: const…
user.io
  • 396
  • 1
  • 6
  • 17
13
votes
3 answers

Is there any performance difference between greater than and greater than or equal?

On today's modern processors, is there any performance difference between greater than and greater than or equal comparison for a branch condition? If I have a condition that could just as easily be either, is there any slight advantage to choosing…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
13
votes
10 answers

Fast integer ABS function

int X = a-b; int d = Math.Abs(X); I am pretty sure that .NET doesn't do inlining. So, will I do if(), or is there some other less-known trick?
Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
13
votes
3 answers

Fastest way to find minimal product of 2 array elements containing 200000+ elements

I have an array a[n]. The number n is entered by us. I need to find the minimal product of a[i] and a[j] if: 1) abs(i - j) > k 2) a[i] * a[j] is minimised Here is my solution (very naive): #include using namespace std; #define ll long…
Mouvre
  • 274
  • 2
  • 10
13
votes
4 answers

Event Handler performance

I have a performance problem. I create 100 new buttons and I want to assign an Click Event Handler. I execute this code for about 100 times: Buttons[i].Button.Click += new System.EventHandler(Button_Click); It takes about 2sec to complete. I have a…
Redax
  • 9,231
  • 6
  • 31
  • 39
13
votes
1 answer

How to speed up dynamic dispatch by 20% using computed gotos in standard C++

Before you down-vote or start saying that gotoing is evil and obsolete, please read the justification of why it is viable in this case. Before you mark it as duplicate, please read the full question. I was reading about virtual machine interpreters,…
user11313931