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
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
246
votes
13 answers

Should I use Java's String.format() if performance is important?

We have to build Strings all the time for log output and so on. Over the JDK versions we have learned when to use StringBuffer (many appends, thread safe) and StringBuilder (many appends, non-thread-safe). What's the advice on using String.format()?…
Air
  • 5,084
  • 5
  • 25
  • 19
183
votes
1 answer

What is the best way to set a register to zero in x86 assembly: xor, mov or and?

All the following instructions do the same thing: set %eax to zero. Which way is optimal (requiring fewest machine cycles)? xorl %eax, %eax mov $0, %eax andl $0, %eax
balajimc55
  • 2,192
  • 2
  • 13
  • 15
140
votes
5 answers

Cost of exception handlers in Python

In another question, the accepted answer suggested replacing a (very cheap) if statement in Python code with a try/except block to improve performance. Coding style issues aside, and assuming that the exception is never triggered, how much…
Thilo
  • 257,207
  • 101
  • 511
  • 656
108
votes
9 answers

When, if ever, is loop unrolling still useful?

I've been trying to optimize some extremely performance-critical code (a quick sort algorithm that's being called millions and millions of times inside a monte carlo simulation) by loop unrolling. Here's the inner loop I'm trying to speed up: //…
108
votes
4 answers

what is faster: in_array or isset?

This question is merely for me as I always like to write optimized code that can run also on cheap slow servers (or servers with A LOT of traffic) I looked around and I was not able to find an answer. I was wondering what is faster between those…
Fabrizio
  • 3,734
  • 2
  • 29
  • 32
106
votes
7 answers

Floating point division vs floating point multiplication

Is there any (non-microoptimization) performance gain by coding float f1 = 200f / 2 in comparision to float f2 = 200f * 0.5 A professor of mine told me a few years ago that floating point divisions were slower than floating point multiplications…
sum1stolemyname
  • 4,506
  • 3
  • 26
  • 44
105
votes
3 answers

Why does my application spend 24% of its life doing a null check?

I've got a performance critical binary decision tree, and I'd like to focus this question on a single line of code. The code for the binary tree iterator is below with the results from running performance analysis against it. public…
Will Calderwood
  • 4,393
  • 3
  • 39
  • 64
87
votes
3 answers

Why can't GCC generate an optimal operator== for a struct of two int32s?

A colleague showed me code that I thought wouldn't be necessary, but sure enough, it was. I would expect most compilers would see all three of these attempts at equality tests as equivalent: #include #include struct Point { …
Ben
  • 9,184
  • 1
  • 43
  • 56
85
votes
8 answers

Fastest way to strip all non-printable characters from a Java String

What is the fastest way to strip all non-printable characters from a String in Java? So far I've tried and measured on 138-byte, 131-character String: String's replaceAll() - slowest method 517009 results / sec Precompile a Pattern, then use…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
81
votes
7 answers

Is it more efficient to perform a range check by casting to uint instead of checking for negative values?

I stumbled upon this piece of code in .NET's List source code: // Following trick can reduce the range check by one if ((uint) index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(); } Apparently this is more efficient (?) than if…
enzi
  • 4,057
  • 3
  • 35
  • 53
80
votes
7 answers

Is it possible to tell the branch predictor how likely it is to follow the branch?

Just to make it clear, I'm not going for any sort of portability here, so any solutions that will tie me to a certain box is fine. Basically, I have an if statement that will 99% of the time evaluate to true, and am trying to eke out every last…
Andy Shulman
  • 1,895
  • 3
  • 23
  • 32
75
votes
6 answers

Is reading the `length` property of an array really that expensive an operation in JavaScript?

I always assumed caching the length of an array in JavaScript is a good idea (especially in the condition of a for loop) because of the expensiveness of calculating the length of an array. Example for (var i = 0; i < arr.length; i++) { } // vs for…
alex
  • 479,566
  • 201
  • 878
  • 984
66
votes
11 answers

Fast method to copy memory with translation - ARGB to BGR

Overview I have an image buffer that I need to convert to another format. The origin image buffer is four channels, 8 bits per channel, Alpha, Red, Green, and Blue. The destination buffer is three channels, 8 bits per channel, Blue, Green, and…
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
64
votes
12 answers

What is the fastest way to find if a number is even or odd?

What is the fastest way to find if a number is even or odd?
aks
  • 4,695
  • 8
  • 36
  • 37
1
2 3
59 60