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

Find the better intersection of two moving objects

I would like to optimize dramaticaly one of my algorithm, i will try to explain it the best way that i can. The subject We are in a 2D euclidian system at the time t = 0. In this system there is two object : O1 and O2. O1 and O2 are respectively…
mamadrood
  • 739
  • 5
  • 12
14
votes
2 answers

Why does GCC keep empty functions?

In most cases if I want to create an optional feature in C, I simply create two functions like this: #ifdef OPTIONAL_SOMETHING void do_something(int n, const char *s) { while (n--) { printf("%s", s); } /* ...You might get the…
Ákos Kovács
  • 502
  • 1
  • 10
  • 23
14
votes
6 answers

Too many Activities in Android?

When i started my Android project i had a misunderstanding that every screen that's shown in the application must be a new activity. Now i am finished with the project , i have checked it on my emulator as well as on a couple of android phones. So…
Desert Ice
  • 4,461
  • 5
  • 31
  • 58
13
votes
1 answer

Quadratic programming in Haskell

Are there any Haskell bindings for quadratic programming libraries? If not, which one should I write simplified bindings for assuming I cannot avoid needing one? Is there a reasonably canonically favored open source library for this?
13
votes
2 answers

Buffer flushing: "\n" vs. std::endl

Possible Duplicate: C++: “std::endl” vs “\n” In Accelerated C++, two things are mentioned: Most systems take a significant amount of time to write characters to an output device. Because of this, C++ accumulates characters to be written in a…
user1253795
13
votes
6 answers

Numerical library for Scala

I'm looking for a library to do numerical computing in Scala (or Java, although something that can use scala functions would be way nicer!) with at least the following capabilities: L-BFGS Minimizers (Powell, QuasiNewton, ...) Numerical…
em70
  • 6,088
  • 6
  • 48
  • 80
13
votes
1 answer

What is the performance impact of using the type class pattern in Scala

I'm currently making extensive use of the type class pattern in to be performance-relevant portions of my code. I made out at least two potential sources of inefficiency. The implicit parameters get passed along message calls. I don't know whether…
ziggystar
  • 28,410
  • 9
  • 72
  • 124
13
votes
4 answers

JPA @ManyToMany join table indexing

Hibernate allows adding indexes on @ManyToOne mappings by the use of @org.hibernate.annotations.Index. Is there a way to specify index for the join table in a @ManyToMany relation? If Entity A and Entity B have a @ManyToMany with A being the owning…
kmansoor
  • 4,265
  • 9
  • 52
  • 95
13
votes
1 answer

What is the name of this generalization of idempotence?

Lots of commonly useful properties of functions have concise names. For example, associativity, commutativity, transitivity, etc. I am making a library for use with QuickCheck that provides shorthand definitions of these properties and others. The…
Doug McClean
  • 14,265
  • 6
  • 48
  • 70
13
votes
3 answers

speed of accessing const variables in c/c++

Is accessing const variables faster than non-const variable? I'm wondering if it is worth using const more as a step in optimizing a program.
13
votes
7 answers

Read a line of input faster than fgets?

I'm writing a program where performance is quite important, but not critical. Currently I am reading in text from a FILE* line by line and I use fgets to obtain each line. After using some performance tools, I've found that 20% to 30% of the time my…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
13
votes
6 answers

Does the order of rules in a CSS stylesheet affect rendering speed?

While this could possibly result in a simple yes or no answer I'll go for it anyway Consider the following example: HTML
Hello world!
Willem
  • 723
  • 2
  • 9
  • 27
13
votes
6 answers

Python: speeding up geographic comparison

I've written some code that includes a nested loop where the inner loop is executed about 1.5 million times. I have a function in this loop that I'm trying to optimize. I've done some work, and got some results, but I need a little input to check if…
Wilduck
  • 13,822
  • 10
  • 58
  • 90
13
votes
6 answers

A fast array shift implementation in C#?

I need to shift to the right and to the left an array by N places. The items that pop out on the side where i shift to must get back into on the other side. Shift right by 13: [0,1,2,3,4,5,6,7,8,9] -> [7,8,9,0,1,2,3,4,5,6] Shift left by…
Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
13
votes
7 answers

How to write a better strlen function?

I am reading "Write Great Code Volume 2" and it shows the following strlen impelementation: int myStrlen( char *s ) { char *start; start = s; while( *s != 0 ) { ++s; } return s - start; } the book says that this…
Victor
  • 1,655
  • 9
  • 26
  • 38