Questions tagged [micro-optimization]

Micro-optimization is the process of meticulous tuning of small sections of code in order to address a perceived deficiency in some aspect of its operation (excessive memory usage, poor performance, etc).

Micro-optimization is the process of meticulous tuning of small sections of code in order to address a perceived deficiency in some aspect of its operation (excessive memory usage, poor performance, etc).

Micro-optimization (and optimization in general) tends to be interesting to programmers because they enjoy finding clever solutions to problems. However, micro-optimization carries the connotation of a disproportionate amount of effort being expended to extract relatively small improvements.

That's not to say that micro-optimization is bad practice in all circumstances. Sometimes a small improvement in a part of a code base that gets used frequently (such as the innermost part of a loop) can yield big overall gains in system performance, and building code for highly constrained systems such as microcontrollers will often require cleverness to eke out the most performance from such a small system.

However, it can be tempting to indulge in the practice where it's not necessary, resulting in a lot of time being spent that could have been used more productively, and in code that is difficult to follow as "clever" solutions to problems are often more difficult to understand than simple solutions, and therefore a micro-optimization can have a negative impact on the maintainability of a piece of code.

Programmers are advised to avoid micro-optimization, unless they can make a solid justification for the problems outlined above being worth the performance gains. Should profiling of the code in question identify a hot-spot that is causing a performance bottleneck, then this can be sufficient justification for a micro-optimization.

900 questions
1
vote
2 answers

Checking a conditional vs setting a variable multiple times; low-level optimization

If I'm searching through a collection of values and running code for each one, and I want to turn a boolean on when I find a certain quality and then back off again when I've run the code for that object, is it faster to run a conditional to check…
c..
  • 1,044
  • 1
  • 9
  • 23
1
vote
1 answer

What exactly is low bits subtraction

I was reading this article and thought that that everything was perfectly clear until I stumble upon this: Again, most real Scheme systems use a slightly different implementation; for example, if GET_PAIR subtracts off the low bits of x, instead of…
P. P
  • 21
  • 2
1
vote
1 answer

x86-64 Relative jmp performance

I'm currently doing an assignment that measures the performance of various x86-64 commands (at&t syntax). The command I'm somewhat confused on is the "unconditional jmp" command. This is how I've implemented it: .global uncond uncond: .rept…
acz
  • 97
  • 10
1
vote
1 answer

The most efficient way of counting positive, negative and zero number using loop unrolling

Say I have the following instruction, simply checks if a number is positive or not (negative or zero) and if it was positive add 1 to our counter (and we don't care if the numbers is negative or zero). I can do this with a simple loop…
Yar
  • 7,020
  • 11
  • 49
  • 69
1
vote
1 answer

Predecoders and decoders. Difference

I am reading Agner Fog's materials and I have some doubts: The pre-decoders and decoders can handle 16 bytes or 4 instructions per clock cycle What is pre-decoders in context of decoders? The author says about cache for macroinstruction. I…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
1
vote
3 answers

Remove if conditions from simple function

I need to remove as many if conditions as possible from the two functions below: inline int inc_with_1bit_saturation(int counter) { if (counter == 1) return --counter; return ++counter; } void branch_prediction_1bit_saturation(int*…
Denis Yakovenko
  • 3,241
  • 6
  • 48
  • 82
1
vote
3 answers

optimizing a lookup

I have an array that i use to lookup values. I use the first 2 values to get n rows. for example all rows that have 2 in the first column and 7 in the second. What is the fastest (i mean micro-optimized) way to get these values? I now use a for loop…
Jeroen
  • 4,023
  • 2
  • 24
  • 40
1
vote
1 answer

How can we optimize CPU bound Java applications?

I'm currently doing some research in Genetic Programming, and I don't have access to multiple computers (yet) such that I can perform aggregate computations. Right now, the research only takes about 1.5hrs on my home desktop so it is not unbearable,…
James
  • 903
  • 7
  • 22
1
vote
1 answer

Declaring/initializing variables inside or outside of a double (or multiple) loop

According to a similar question, and multiple Google searches, declaring or initializing a variable outside or inside a loop "might" be the same in terms of performance. I am running a computer simulation, and I am trying to optimize the code as…
1
vote
3 answers

Is there any benefit to replacing loop indexers with pointers?

Assuming: const int n = bigNumber; float* f = someData; Is there any point in converting for (int i = 0; i < n; i++) { f[i] += 1; } to float* lastF = f + n; for (float* i = f; i < lastF; i++) { *i += 1; } Naively looking at this, it seems…
Rotem
  • 21,452
  • 6
  • 62
  • 109
1
vote
2 answers

Detection clang on different platforms

Faced with the problem of parsing version of clang for different vendors clang --version|head -1 Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn) => 3.5 FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512 =>…
Denis Denisov
  • 661
  • 8
  • 17
1
vote
6 answers

Counting down to zero in contrast to counting up to length - 1

Is it recommended to count in small loops (where possible) down from length - 1 to zero instead of counting up to length - 1? 1.) Counting down for (int i = a.length - 1; i >= 0; i--) { if (a[i] == key) return i; } 2.) Counting up for (int i =…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
1
vote
2 answers

Javascript scope chain

I am trying to optimize my program. I think I understand the basics of closure. I am confused about the scope chain though. I know that in general you want a low scope (to access variables quickly). Say I have the following object: var my_object =…
Geromey
  • 55
  • 3
1
vote
1 answer

How much bytes instance of my class uses?

How big is instance of following class after constructor is called? I guess this can be written generally as size = nx + c, where x = 4 in x86, and x = 8 in x64. n = ? c = ? Is there some method in .NET which can return this number? { byte[][]…
watbywbarif
  • 6,487
  • 8
  • 50
  • 64
1
vote
0 answers

Optimization of number grinding

Long story short, i have to solve 20..200 block-tridiagonal linear systems during an iterational process. Size of systems is 50..100 blocks, 50..100 x 50..100 each. I will write down here my thoughts on it, and i ask you to share your opinion on my…
Dantragof
  • 155
  • 6