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

How can I optimize this power-up program so that I don't get so much RAW?

I have a problem with this code, I'm running it on winMips64 and I'm getting a lot of RAW errors I'm new with this kind of coding and I'm still trying to learn it .data n: .word 8 x: .double 0.5 .text LD R1,n(R0) L.D …
mikeyMike
  • 21
  • 4
0
votes
1 answer

string_view Vs const char* performance

Is a std::string_view parameter better than a const char* one in the code below? void func( const std::string_view str ) { std::istringstream iss( str.data( ) ); // str is passed to the ctor of istringstream std::size_t pos { }; int…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0
votes
4 answers

When joining four 1-byte vars into one 4-byte word, which is a faster way to shift and OR ? (comparing generated assembly code)

So I'm currently studying bit-wise operators and bit-manipulation, and I have come across two different ways to combine four 1-byte words into one 4-byte wide word. the two ways are given below After finding out this two methods I compare the…
0xdeadbeef
  • 500
  • 3
  • 17
0
votes
1 answer

C++20 Likely and UnLikely?

I was reading https://iq.opengenus.org/cpp-likely-and-unlikely-attributes/ and I don't understand/agree with few things. In the following code: void doModulus( vector &vec , int mod ){ // here the value of mod we are passing is 224, vec is…
Algo
  • 23
  • 1
0
votes
1 answer

ARM Assembly, Evaluate Alternative Latencies, How Close Matters?

When evaluating two alternatives to solve a problem, comparing clock-cycles and latencies, if both evaluate roughly the same, is there a better way to decide which to use? Example - Converting to Hexstring An example I was looking at involves…
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0
votes
1 answer

Does specifying array length when initializing affect compile time?

I understand that at runtime, const char text[] = "some char array" is the same as const char text[16] = "some char array". Is there any difference in compile time? I reckon there would be, as telling the compiler how many elements there are in the…
0
votes
1 answer

Optimizing a C function call using 64-bit MASM

Currently using this 64-bit MASM code to call a C runtime function such as memcmp(). I recall this convention was from a GoAsm article on optimizations. memcmp PROTO;:QWORD,:QWORD,:QWORD PUSH RSP …
vengy
  • 1,548
  • 10
  • 18
0
votes
0 answers

How would I rewrite the for statements in this function to be more performant?

I have the following function: ColorPalette._processPaletteImageData = function() { const paletteImage = this._bmp(); const source = this._source; if (paletteImage) { const _colorPicker = (_n) => { const pX =…
0
votes
1 answer

can the mips pseudo-instruction la be replace by a single MIPS instruction?

Can the instruction la $4,-16($9) be replaced with a single MIPS machine instruction? I've been searching and every time I find a combination of lui and ori.
user1286550
  • 101
  • 1
  • 6
0
votes
1 answer

The program perform a loop to sum up all the value in the input. How can I optimize the loop part in MIPS?

input: .word 71 22 13 64 55 46 98 main: addi $t0, $zero, 7 # line 1 addi $t1, $zero, 0 # line 2 addi $t7, $zero, 0 # line 3 Loop: sll $t2, $t1, 2 # line 4 t2 = 0 addi $t1, $t1, 1 # line 5…
0
votes
1 answer

How to optimize the algorithm of this program(write in asm)

The aim of the programme is to add the Hex data from ffff:0-ffff:b The result of the add will be saved as dx assume cs:code code segment mov ax,0ffffH ; set ds mov ds,ax ; point to ffffH mov ax,4000H ; set ss mov ss,ax …
Chris
  • 23
  • 2
0
votes
4 answers

Which is more efficient between __autoload() or include/require?

Which has faster execution time: __autoload or include inside nested if statements? Which is less error prone between the two?
Karina
  • 948
  • 2
  • 13
  • 26
0
votes
2 answers

u8 as i32 vs i32::from_be_bytes([u8;4])

Total bike-shedding here, but I'm wondering about the relative efficiency of let mut buf = [0u8]; input.read(&mut buf)?; Ok(buf[0] as i32) vs let mut buf = [0u8; 4]; input.read(&mut buf[3..4])?; Ok(i32::from_be_bytes(buf)) Even on aesthetic…
Don Hosek
  • 981
  • 6
  • 23
0
votes
0 answers

Optimizing given assembly code for ARM using instruction sets

I was going through some past school assessments & came across this rather interesting question. The question asks to optimize said assembly code, I've written out the pseudocode in C (for reference). The catch here is that my Professor said it's…
waffledood
  • 193
  • 8
0
votes
0 answers

Calculate simple formula with only one register

I would like to calculate the following formula very fast and ONLY with EAX: eax = ( eax - 0x01010101 ) AND (NOT eax) Is this possible? What do the assembler specialists think? I get stuck myself because assembler is not my programming language.
Teddy
  • 19
  • 1