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

Efficiency: Objects inside arrays or arrays inside objects?

Given the same number of array/object layers, each index indicating the same things, what is the best order to nest the arrays and objects in? I'm making a grid-based game, and I need to store several pieces of information about each square. I know…
RoboticRenaissance
  • 1,130
  • 1
  • 10
  • 17
0
votes
1 answer

Slow Workbook_Open event

I have a workbook that takes more than 6 seconds to open due to a number of macros that run within the workbook_open event. I want to speed this up so I have used a timer to test different parts of the code at startup vs being run while the workbook…
matt9292
  • 401
  • 2
  • 7
  • 19
0
votes
1 answer

In Javascript, how do I obtain a reference to an element in an array?

I have a piece of code like if (A[i][j] === 1) A[i][j] = 0; else A[i][j] = 1; and I'm wondering how I can optimize it like var elem = A[i][j]; // obviously A[i][j] is an R-value ... I want an L-value if (elem === 1) elem = 0; else …
0
votes
1 answer

Which operator is the fastest in Javascript to check whether 2 values are equal to 0?

Which of the following is fastest and what is the justification for why? (1) if (x == 0 and y == 0) (2) if ((x | y) == 0) (3) Other (Please mention. If any)
0
votes
2 answers

Is there another way to calculate (a^3) * b + 5*(b^2) without so many mul instructions?

I'm starting to learn assembly, could someone please review my solution? Exercise : You are given eax=a, ebx=b. Calculate (a^3) * b + 5*(b^2), and store the result inside eax. (Here * means multiplication, and c^d means c to the power of…
Bruno
  • 348
  • 5
  • 21
0
votes
1 answer

What effect will branch prediction have on the following C loop?

My experience with C is relatively modest, and I lack good understanding of its compiled output on modern CPUs. The context: I'm working on image processing for an Android app. I have read that branch-free machine code is preferred for inner loops,…
0
votes
3 answers

How to copy bytes from memory using pattern (YUYV packed to YUV420 planar)

Let's start with this: I have a block of memory of 16 bytes and I need to copy only even bytes to a 8 bytes block of memory. My current algorithm is doing something like this: unsigned int source_size = 16, destination_size = 8, i; unsigned char *…
0
votes
1 answer

use log value to represent scientific value

I recently read some java scientific code which needs to do a lot of multiplications. The implementation use log value because the developer thought it's faster and preciser. For example, If he/she needs to calculate double values…
Frank
  • 7,235
  • 9
  • 46
  • 56
0
votes
1 answer

Java primitive comparison performance

I'm comparing int a and int b. Is there any performance difference between if(a>=b){...} vs if(a==b || a>b){...}? Thanks
elmoz
  • 13
  • 1
0
votes
1 answer

Multiple inner assignments - how it works?

I'm trying to do a "1-liner" for fun purposes and for some reason i can't make the code work properly. Note: that's C, not C++, however the issue persists in both. The "unfolded" version: #include #include int main() { double…
keke
  • 105
  • 2
  • 2
  • 14
0
votes
8 answers

Performance when accessing class members

I'm writing something performance-critical and wanted to know if it could make a difference if I use: int test( int a, int b, int c ) { // Do millions of calculations with a, b, c } or class myStorage { public: int a, b, c; }; int test(…
Dr. Acula
  • 449
  • 1
  • 5
  • 6
0
votes
1 answer

Comparing speed of & vs % in C++

Is it true that in C++ command n & 1 is quicker and uses less memory than n % 2? (where n is of type int) More globally, is there any way to find residue of an integer modulo 2 quicker than using % operator? Thanks in advance.
Igor
  • 179
  • 2
  • 7
0
votes
1 answer

code optimization results in slower execution - need explanation

public class Zigzag{ public static void zigzag_optimizated(int n, int m) { int length = 2*m; int localIndex[] = new int[n]; for(int i = 0; i
cdxf
  • 5,501
  • 11
  • 50
  • 65
0
votes
1 answer

Optimize css vs Google page speed is messing with me

I'm using google page speed and it's telling me my css is inefficient... Very inefficient rules (good to fix on any page): * table.fancy thead td Tag key with 2 descendant selectors and Class overly qualified with tag * table.fancy tfoot td …
0
votes
6 answers

Overhead of calling tiny functions from a tight inner loop? [C++]

Say you see a loop like this one: for(int i=0; i
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589