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

How can I accelerate this code (MWE!), e.g. using restrict

Is there any way I can accelerate this function: void task(int I, int J, int K, int *L, int **ids, double *bar){ double *foo[K]; for (int k=0;k
Bananach
  • 2,016
  • 26
  • 51
-2
votes
4 answers

for loop or while loop which is faster?

Possible Duplicate: What loop is faster, while or for we can use for loop as well as while loops for same purpose which is faster for eg: i want to loop an item 1000000000 times shall i use for loop or while loop? it is iteration then why do we…
Nighil
  • 4,099
  • 7
  • 30
  • 56
-2
votes
1 answer

Previous even number

If an integer is uneven (odd), I would like to get the previous number, otherwise I would like to keep the current number. E.g. if x = 3, I would like to assign 2 to x, if x = 4, then nothing happens. At the moment, I do the following: x = (x/2)*2,…
YnkDK
  • 681
  • 9
  • 26
-2
votes
1 answer

Which is faster in PHP: !! or (bool)?

I am trying to microptimize the code and I was wondering which is the faster to convert a variable into a boolean: I am not worry about code size, just about execution time. some benchmark…
Fabrizio
  • 3,734
  • 2
  • 29
  • 32
-2
votes
2 answers

micro optimisation in for loop c++

Let's assume C++ for loop in the form of: for(int i; i<10;i++) So the integer needs to be allocated in the beginning and then increased at every step the as well as compared. So wouldn't it be faster to do something like that: f or(int i;…
magu_
  • 4,766
  • 3
  • 45
  • 79
-3
votes
1 answer

Performance of inequality operator

It appears that (!$a == 'hello') is consistently faster than ($a != 'hello') // (!$a == 'hello') Used time: 52.743232011795 Used time: 52.633831977844 Used time: 51.452646970749 //($a != 'hello') Used time: 76.290767908096 Used time:…
mixdev
  • 2,724
  • 2
  • 30
  • 25
-3
votes
2 answers

Ternary operator vs array?

In C, is indexing an array faster than the ?: operator? For example, would (const int[]){8, 14}[N > 10] be faster than N > 10? 14 : 8?
user15170464
-3
votes
4 answers

How eliminate duplicate cases from a switch statement in PHP

I'm making a function to return whether or not the given user_id is a staff member of the site. This is what I have and it works, however I feel like it can be greatly improved. public function isUserStaff($uid) { $stmt =…
-3
votes
3 answers

C++ code optimization using expressions instead of variables

I have a question about creating optimal C++ program. I have a function that computes expressions like: c= a/2 c = (a*b)/2 c = (a/2) + b etc. is it better to use a variable to store these values or just use return ? I understand…
weu833
  • 1
-4
votes
1 answer

micro optimization: x&1 vs. x%2

In terms of speed, which is fastest and why ? x&1 //checks last bit or x%2 // performs modulo operation
gelatine1
  • 199
  • 1
  • 3
  • 11
-4
votes
1 answer

cost of == operator vs < or > operators

This is really just sort of an academic question, I'm just curious to know which one is faster. I'm guessing the difference is negligible but still, I'd like to know. if( (x == 1) || (x == 2) ) vs if (x < 3) thanks!
Vincent
  • 103
  • 7
-5
votes
1 answer

Most Efficient Way to Compare 2 Primitives in Java?

Which code is better or optimized or efficient? double a; double b; if (a == b) return true; or if (a - b == 0) return true;
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19
-5
votes
2 answers

Is it good practice to construct long circuit statements?

Question Context: [C++] I want to know what is theoretically the fastest, and what the compiler will do. I don't want to hear about premature optimization is the root of all evil, etc. I was writing some code like this: bool b0 = ...; bool b1 =…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
-7
votes
2 answers

Micro-optimizations: if($var){ ... } vs if($var): ... endif

Is if($var){ ... } faster than if($var): ... endif ? Which one do you use?
-8
votes
2 answers

What code is more CPU expensive: while(*p) or while(i--)?

What C code is more CPU expensive: while(*pointer){ pointer++; } or while(counter > 0){ pointer++; counter--; } ?
legale
  • 612
  • 7
  • 9
1 2 3
59
60