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
votes
1 answer

Delete a node from a linked list in C

I'm a CS student and I'm just started to learn about linked lists. I want to write the simplest code that deletes a node from a linked list, which means that I won't take into account any special cases; like what if the element is at the head or…
-1
votes
1 answer

High Performance Bit Removal (XOR vs. subtract)

It is my understanding that XOR messes with branch prediction. Is it preferable to remove bits via subtraction or via xor for operations that will run a great many times? // For an operation that will run several million times ... int encoding =…
B. Nadolson
  • 2,988
  • 2
  • 20
  • 27
-1
votes
1 answer

Check if ax is divisible by 16

How can we check if ax is divisible by 16? I know we can by this command: AND 0x000f There is a faster command? (I think idiv is slower)
user15051177
-1
votes
1 answer

How to copy a register and do `x*4 + constant` with the minimum number of instructions

I am new to x86 assembly. For example for the following instruction: multiply the contents of ESP by 4 and add 0x11233344, storing the result in EDI. How do I represent this instruction in x86 assembly with minimum number of instructions? push…
Sean
  • 1
  • 1
-1
votes
1 answer

Most efficient way to find the greatest number not greater than A, which is divisible by B

I have 2 numbers A and B. I want to find C = A - (A % B), but there are some problems. First of all, if C and D = A / B should have the same parity ((even and even) or (odd and odd)), otherwise C should be incremented (++C). The second problem is…
Bioliquid
  • 161
  • 7
-1
votes
1 answer

Unsure if passing by reference is working

I've created a library for fun called GamblersDice. I'm trying to micro-optimize it but unsure if I'm doing it right. What I want is to use a reference to a global Random object when creating the die. The reason I believe it's not working is that…
-1
votes
1 answer

How can I optimize this graphic-sprite-emulation routine?

This routine is a windowed get/put-sprite emulation-routine. I'm Italian and I've not translated them. This routine is capable to flip-x-y the image before storing it in a 16 Byte aligned buffer. I omitted the precalculation routine for reasons of…
-1
votes
1 answer

Optimizing per-thread copying and 0-padding of data in registers/L1

I'm writing a kernel which, among other things, has each thread populate one variable with data constituting the lower bytes, and pads the rest (assuming little-endianness). This is done repeatedly and non-uniformly across threads, in that some…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
-1
votes
2 answers

Java: Is it possible to Optimize double comparisons/multiplications?

I've been stuck on a very concerning problem with my physics engine, and it's that I'm having trouble calculating for thousands of Collisions all at once. I've optimized the method responsible for telling me collision so much, it creates 0 objects…
-1
votes
1 answer

Css utils class performance

I see the trend of css utils class and I am wondering if there is an impact on performance for this style. What will be a better way? One - make your html element with bunch of utils classes like:
user233232
  • 6,067
  • 9
  • 29
  • 37
-1
votes
2 answers

for-loop(downto,Reversed) vs for-loop(increase mode) vs while-loop in Delphi

I have a micro-optimization issue. I have 3 methods for processing typed-Pointer(array) . Which one is better? 1 for I:=0 to ArrCount-1 do begin // I:Var is unused in below-block Inc(P) ; // P is typed-Pointer // do somethings end; 2 for…
MajidTaheri
  • 3,813
  • 6
  • 28
  • 46
-1
votes
1 answer

Why does running my tests in a different order result in drastically different performances?

I made a program that, for a variety of methods of dividing ints and returning the double result, runs this method for every possible combination of numerators and denominators between 0 and 20, inclusive, and it gave these results for the amount of…
-1
votes
4 answers

PHP: Which is faster: do while, while, or for loop

I am currently creating a socket server in PHP and I would like to know which one would be faster to use throughout it. I've heard for loops are faster than while loops, but I don't know about do whiles. Thanks
Tybone Ten
  • 29
  • 1
  • 2
-2
votes
5 answers

while (n > 1) is 25% faster than while (n)?

I have two logically equivalent functions: long ipow1(int base, int exp) { // HISTORICAL NOTE: // This wasn't here in the original question, I edited it in, if (exp == 0) return 1; long result = 1; while (exp > 1) { if…
orlp
  • 112,504
  • 36
  • 218
  • 315
-2
votes
1 answer

Can this MIPS assembly code be simplified?

I'm trying to write a function that counts the length of a string with the fewest number of dynamic instructions. This is what I have as of now: strlen: li $v0, 0 # len = 0 top: lbu $t0, 0($a0) # get…
jiyoon
  • 201
  • 4
  • 14
1 2 3
59
60