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

EASTL versus STL, how can there be such a performance difference in std::vector::operator[]

According to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html vector::operator[] is between 2% and 70% faster in EASTL than a "commonly used commercial version of STL". Unless the commercial version of STL uses range…
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
13
votes
3 answers

What prevents the usage of a function argument as hidden pointer?

I try to understand the implication of System V AMD64 - ABI's calling convention and looking at the following example: struct Vec3{ double x, y, z; }; struct Vec3 do_something(void); void use(struct Vec3 * out){ *out = do_something(); } A…
ead
  • 32,758
  • 6
  • 90
  • 153
13
votes
2 answers

Fast way to group variables based on direct and indirect similarities in multiple columns

I have a relatively large data set (1,750,000 lines, 5 columns) which contains records with unique ID values (first column), described by four criteria (4 other columns). A small example would be: # example library(data.table) dt <-…
R. Lima
  • 412
  • 4
  • 15
13
votes
1 answer

Scipy minimize: How to pass args to both the objective and the constraint

My MWE is as follows def obj(e, p): S = f(e) + g(p) return S I would like to minimize this function over only e and pass p as an argument to the function. However, I also would like a constraint that depends on p and e that is of the form p…
user1936752
  • 756
  • 1
  • 9
  • 25
13
votes
2 answers

What I Have Learned About Unity ScrollRect / ScrollView Optimization / Performance

ScrollView performance is a real drag (get it?), especially on mobile platforms. I frequently found myself getting less that 15 fps which left the user experience feeling jolting and underwhelming. After a lot of research and testing I have compiled…
Phedg1
  • 1,888
  • 3
  • 18
  • 35
13
votes
2 answers

GCC generates test for nonzeroness even if the zero value is guaranteed

Consider the following class, created mainly for benchmarking purposes: class String { char* data_; public: String(const char* arg = "") : data_(new char[strlen(arg) + 1]) { strcpy(data_, arg); } String(const String& other) :…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
13
votes
4 answers

profile-guided optimization (C)

Anyone know this compiler feature? It seems GCC support that. How does it work? What is the potential gain? In which case it's good? Inner loops? (this question is specific, not about optimization in general, thanks)
elmarco
  • 31,633
  • 21
  • 64
  • 68
13
votes
1 answer

What is that optimization algorithm called?

I've got a piece of undocumented code, which I have to understand to fix an error. The following method is called optimization and it is supposed to find the maximum of a very complex function f. Unfortunately, it fails under some circumstances…
Stanley F.
  • 1,846
  • 16
  • 29
13
votes
2 answers

Partitioning data on a variable to speed up "fuzzy match" using stringdist

I am building on an answer provided to a previous question about fuzzy matching using stringdist. I have two large datasets (~30k rows) with long strings (consumer product names) that I want to fuzzy match by generating a distance score. There is…
roody
  • 2,633
  • 5
  • 38
  • 50
13
votes
8 answers

Minimize the sum of errors of representative integers

Given n integers between [0,10000] as D1,D2...,Dn, where there may be duplicates, and n can be huge: I want to find k distinct representative integers (for example k=5) between [0,10000] as R1,R2,...,Rk, so the sum of errors of all the…
outlaw
  • 1,133
  • 1
  • 8
  • 17
13
votes
6 answers

Is using string.length() in loop efficient?

For example, assuming a string s is this: for(int x = 0; x < s.length(); x++) better than this?: int length = s.length(); for(int x = 0; x < length; x++) Thanks, Joel
Joel
  • 15,166
  • 16
  • 39
  • 31
13
votes
4 answers

sorting int array with only 3 elements

I have this array: int [] myarray = {17, 6, 8}; What is the optimal way to sort this array, in pseudocode? Thanks!
clamp
  • 33,000
  • 75
  • 203
  • 299
13
votes
5 answers

Optimize C# file IO

Scenario - 150MB text file which is the exported Inbox of an old email account. Need to parse through and pull out emails from a specific user and writes these to a new, single file. I have code that works, its just dogged slow. I'm using marker…
paparush
  • 1,340
  • 1
  • 17
  • 25
13
votes
3 answers

Getting Floor Division and Remainder at same time in 2 separate variables

Is there a python built-in (or just optimized) function to get the floor division and remainder at the same time in two separate variables? Example: a, b = 10 divided by 4 Desired results: a = 2 b = 2 I need this to be an optimized…
joaoavf
  • 1,343
  • 1
  • 12
  • 25
13
votes
1 answer

Why can't my DQN agent find the optimal policy in a non-deterministic environment?

edit: The following seems also to be the case for FrozenLake-v0. Please note that I'm not interested in simple Q-learning as I want to see solutions that work with continuous observation spaces. I recently created the banana_gym OpenAI environment.…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958