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

Optimisation of a write out of a loop

Moving a member variable to a local variable reduces the number of writes in this loop despite the presence of the __restrict keyword. This is using GCC -O3. Clang and MSVC optimise the writes in both cases. [Note that since this question was…
JCx
  • 2,689
  • 22
  • 32
13
votes
8 answers

What's a good C++ library for matrix operations

I need to do multiplication on matrices. I'm looking for a library that can do it fast. I'm using the Visual C++ 2008 compiler and I have a core i7 860 so if the library is optimized for my configuration it's perfect.
user558209
  • 141
  • 1
  • 1
  • 6
13
votes
1 answer

How to effectively filter tree view retaining its existing structure?

I am having a tree structured JSON which should be filtered and the result should retain the tree structure. var tree = [ { text: "Parent 1", nodes: [ { text: "Child 1", type: "Child", nodes: [ { …
Surabhil
  • 355
  • 1
  • 3
  • 16
13
votes
2 answers

optimization - stepping may behave oddly : iOS/Unity

I am trying to integrate unity to iOS application. I have followed this tutorial http://www.agnosticdev.com/blog-entry/swift/integrating-unity-and-vuforia-ios-swift-project Now after integrating when i start my app it crashes and show this error on…
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
13
votes
6 answers

How is it possible to see C# code after compilation/optimization?

I was reading about the yield keyword when I came across a sample chapter from C# in Depth: http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx. The first block of code utilizes the yield keyword to make a simple iterator.…
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
13
votes
1 answer

How does mtune actually work?

There's this related question: GCC: how is march different from mtune? However, the existing answers don't go much further than the GCC manual itself. At most, we get: If you use -mtune, then the compiler will generate code that works on any of…
Marc.2377
  • 7,807
  • 7
  • 51
  • 95
13
votes
2 answers

What's the most efficient way to calculate the warp id / lane id in a 1-D grid?

In CUDA, each thread knows its block index in the grid and thread index within the block. But two important values do not seem to be explicitly available to it: Its index as a lane within its warp (its "lane id") The index of the warp of which it…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
13
votes
1 answer

How to generate random numbers faster in R?

My project requires generating a substantial amount (in the order of 10^7, for example) of random numbers that is normally distributed, and this step takes a significant amount of computational resources. Is there a more efficient method than rnorm…
Lei_Xu
  • 155
  • 7
13
votes
3 answers

Fast I/O in c, stdin/out

In a coding competition specified at this link there is a task where you need to read much data on stdin, do some calculations and present a whole lot of data on stdout. In my benchmarking it is almost only i/o that takes time although I have tried…
Daniel Falk
  • 522
  • 4
  • 16
13
votes
1 answer

How to measure CSS parsing and rendering time in a browser?

I have a large SPA with a single large CSS file which contains many rules. Some of them are outdated and should be refactored or removed. It is compiled from a set of SCSS source files. I am now refactoring styles and wonder if there is a way to…
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
13
votes
2 answers

How can several comparisons be slower than some calculations?

We were developing a piece of code that would check whenever the user should not be allowed to get into a sector within a time period, one of my colleagues created a function which in the code below is the isAllowed and contains several comparisons,…
Mike
  • 175
  • 10
13
votes
7 answers

A better 8x8 bytes matrix transpose with SSE?

I found this post that explains how to transpose an 8x8 bytes matrix with 24 operations, and a few scrolls later there's the code that implements the transpose. However, this method does not exploit the fact that we can block the 8x8 transpose into…
xmas79
  • 5,060
  • 2
  • 14
  • 35
13
votes
3 answers

Faster numpy-solution instead of itertools.combinations?

I'm using itertools.combinations() as follows: import itertools import numpy as np L = [1,2,3,4,5] N = 3 output = np.array([a for a in itertools.combinations(L,N)]).T Which yields me the output I need: array([[1, 1, 1, 1, 1, 1, 2, 2, 2, 3], …
Khris
  • 3,132
  • 3
  • 34
  • 54
13
votes
1 answer

Slow recursion in python

I know this subject is well discussed but I've come around a case I don't really understand how the recursive method is "slower" than a method using "reduce,lambda,xrange". def factorial2(x, rest=1): if x <= 1: return rest else: …
Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
13
votes
3 answers

Android M startActivity battery optimization

I'm developing an app that should alert an user if is near a place. and of course have to do that also if the phone is in idle. With DOZE now I understood that I have to whitelist my app, and to do that I saw that I can start an intent with the…
Ast
  • 337
  • 1
  • 4
  • 17