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
0
votes
0 answers

Efficient comparison of unsigned ints (C++ compiler optimization)

I want to get the most speed-optimal code out of this frequently run comparison: int Object::Compare(Object *other) { if(id < other->id) return -1; if(id > other->id) return 1; return 0; } The id var is an unsigned 32-bit integer. What…
Lummox JR
  • 59
  • 6
0
votes
0 answers

Is there a way to hint the compiler to compute the entire value for multiple fields?

I have following C# code: public struct Color { public byte R; public byte G; public byte B; public byte A; } public static Color F() { var c = new Color(); c.R = 0xAA; c.G = 0xBB; c.B = 0xCC; c.A = 0xDD; …
user12722843
0
votes
0 answers

Rewrite the mips code to make it be the fastest

I am going to rewrite a simple mips code that I need to make it become the fastest version. I don’t know how can I optimise this code and I want to get some help .data a:.byte0 b:.byte0 c:.byte0 .text abc: lb $t0, 0($s0) addi $t0, $t0, 1 sb $t0,…
0
votes
2 answers

Optimization in C

I've been trying to optimize some simple code and I try two kind of optimizations, loop enrolling and memory aliasing. My original code: int paint(char *dst, unsigned n, char *src, char bias) { unsigned i; for (i=0;i
user15051177
0
votes
1 answer

Directly increase the exponent of a double in order to multiply by a power of two

I want to increase the exponent of a primitive double d, thus multiplying it by a power of two, for example 2^64. I can do d *= math.pow(2, 64) or calculate the power in advance if known: d *= 18446744073709551616. But I guess this is doing some…
talz
  • 1,004
  • 9
  • 22
0
votes
1 answer

Final variables in onDraw() method

I have a class that extends View and overrides the onDraw(Canvas canvas) method. This view runs animations, so onDraw will be called many times per second. Consider the following example... @Override protected void onDraw(Canvas canvas) { final…
dfetter88
  • 5,381
  • 13
  • 44
  • 55
0
votes
1 answer

Does the position of a function/method in a program matters in case of increasing/ decreasing the speed at lower level(memory)?

Let say I write a program which contains many functions/methods. In this program some functions are used many times as compared to others. In this case does the positioning of a function/method matters in terms of altering the speed at lower…
0
votes
0 answers

ARM Assembly Optimisation - Basic

LDR R1, a LDR R2, b MOV R0, #0 loop ADD R0, R0, R1 SUB R2, R2, #1 CMP R2, #0 BNE loop STR R0, c State two cases where the program could produce an erroneous result, and show how each case could be detected by modifications to the program. Give…
lobit
  • 55
  • 6
0
votes
1 answer

manipulating mips assembly code to decrease cache miss rate (mars simulator)

How could an assembly code be optimised to decrease the miss rate of the cache? I am aware that changing the placement policy/block size/block replacement policy has effects on the cache miss rate, but I am specifically asking for the settings on…
0
votes
1 answer

The fastest way to follow final URL after multiple redirects with PHP

What i want to achieve is to get the final URL based on a first URL (some hardcoded affiliate hyperlinks). Let's say that the redirect chain can look something like this (basically there are max 3 redirects): http://example.com/jhishsuisasd…
Madalin
  • 21
  • 3
0
votes
1 answer

Efficient way to calculate a fraction only if denominator is non-zero

For a machine learning application, I need to sort lists based on a value that gets updated but is initialized to 0. The problem is that parts of the updated values initially 0 are used int he formula that's used as sort-key, one of which in the…
jaaq
  • 1,188
  • 11
  • 29
0
votes
1 answer

how to zero out all the registers in assembly without multiple instructions?

im trying to clear all the registers using this assembly code: xor eax, eax xor ebx, ebx xor ecx, ecx xor edx, edx xor esi, esi xor edi, edi but is there any other effective way, like a single instruction or anything else
hakim
  • 33
  • 4
0
votes
0 answers

Y86 Architecture Immediate VS Register Arithmetic Efficiency Question

I am working with a team in a Computer Architecture class on a Y86 program to implement multiplication function imul. We have a block of code that works, but we are trying to make it as execution-time efficient as we can. Currently our block looks…
0
votes
1 answer

Broadcasting DWORD to YMM

I am just wondering if the below code: mov eax, r9d ; eax = j mul n ; eax = n * j shl eax, 2 ; eax = 4 * n * j ; now I want to 'broadcast' this to YMM, like so: ; ymm = { eax, eax, eax, eax, eax,…
weno
  • 804
  • 8
  • 17
0
votes
0 answers

What exactly xchg ax, ax aligns for?

I've seen various comments on the internet about xchg ax, ax being used for alignment purposes (I know there are other as well, but let's forget about them for now), e.g. loop alignment. If stuff I've read is true, what is the reasoning for such…
Etki
  • 2,042
  • 2
  • 17
  • 40