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

Optimum way to compare strings in JavaScript?

I am trying to optimize a function which does binary search of strings in JavaScript. Binary search requires you to know whether the key is == the pivot or < the pivot. But this requires two string comparisons in JavaScript, unlike in C like…
HRJ
  • 17,079
  • 11
  • 56
  • 80
509
votes
28 answers

Most efficient way to increment a Map value in Java

I hope this question is not considered too basic for this forum, but we'll see. I'm wondering how to refactor some code for better performance that is getting run a bunch of times. Say I'm creating a word frequency list, using a Map (probably a…
gregory
  • 9,464
  • 4
  • 18
  • 13
503
votes
8 answers

How do I add indexes to MySQL tables?

I've got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run SELECT * FROM table WHERE id = '1'; the code runs fine as the ID field is the primary index. However, for a recent development in the project, I have…
Michael
  • 5,505
  • 4
  • 20
  • 12
423
votes
3 answers

How to identify unused CSS definitions from multiple CSS files in a project

A bunch of CSS files were pulled in and now I'm trying to clean things up a bit. How can I efficiently identify unused CSS definitions in a whole project?
jswanson
  • 16,244
  • 6
  • 20
  • 15
418
votes
25 answers

Fastest sort of fixed length 6 int array

Answering to another Stack Overflow question (this one) I stumbled upon an interesting sub-problem. What is the fastest way to sort an array of 6 integers? As the question is very low level: we can't assume libraries are available (and the call…
kriss
  • 23,497
  • 17
  • 97
  • 116
416
votes
22 answers

Which is better option to use for dividing an integer number by 2?

Which of the following techniques is the best option for dividing an integer by 2 and why? Technique 1: x = x >> 1; Technique 2: x = x / 2; Here x is an integer.
Abhineet
  • 6,459
  • 10
  • 35
  • 53
409
votes
7 answers

Why does the order of the loops affect performance when iterating over a 2D array?

Below are two programs that are almost identical except that I switched the i and j variables around. They both run in different amounts of time. Could someone explain why this happens? Version 1 #include #include main () { …
Mark
  • 8,408
  • 15
  • 57
  • 81
396
votes
40 answers

Fastest way to list all primes below N

This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))) …
jbochi
  • 28,816
  • 16
  • 73
  • 90
392
votes
28 answers

A weighted version of random.choice

I needed to write a weighted version of random.choice (each element in the list has a different probability for being selected). This is what I came up with: def weightedChoice(choices): """Like random.choice, but each element can have a…
Colin
  • 10,447
  • 11
  • 46
  • 54
368
votes
6 answers

How to see which plugins are making Vim slow?

Is there a way to profile Vim plugins? My MacVim becomes slower and slower when I open a large .py. I know I could deselect all plugins and reselect one by one to check which plugin is the culprit, but is there a faster way? My dotvim is here:…
charlax
  • 25,125
  • 19
  • 60
  • 71
345
votes
18 answers

Most efficient way to concatenate strings?

What's the most efficient way to concatenate strings?
jimmij
  • 3,501
  • 2
  • 19
  • 6
344
votes
4 answers

Deoptimizing a program for the pipeline in Intel Sandybridge-family CPUs

I've been racking my brain for a week trying to complete this assignment and I'm hoping someone here can lead me toward the right path. Let me start with the instructor's instructions: Your assignment is the opposite of our first lab assignment,…
Cowmoogun
  • 2,507
  • 4
  • 12
  • 17
321
votes
13 answers

How do I position one image on top of another in HTML?

I'm a beginner at rails programming, attempting to show many images on a page. Some images are to lay on top of others. To make it simple, say I want a blue square, with a red square in the upper right corner of the blue square (but not tight in…
rrichter
  • 9,253
  • 5
  • 23
  • 13
319
votes
7 answers

Why does this code execute more slowly after strength-reducing multiplications to loop-carried additions?

I was reading Agner Fog's optimization manuals, and I came across this example: double data[LEN]; void compute() { const double A = 1.1, B = 2.2, C = 3.3; int i; for(i=0; i
ttsiodras
  • 10,602
  • 6
  • 55
  • 71
319
votes
19 answers

How can I know which parts in the code are never used?

I have legacy C++ code that I'm supposed to remove unused code from. The problem is that the code base is large. How can I find out which code is never called/never used?
user63898
  • 29,839
  • 85
  • 272
  • 514