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

CPU-Core thread classification Function

I'm going to be writing a multi-threaded shared memory messaging system for ultra high-volume message delivery between processes. The messages will originate from the worker threads of a web-server. I'd like to exploit the CPU cache locality that…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
1
vote
2 answers

Appending Strings vs appending chars in Java

I am trying to solve an algorithmic task where speed is of primary importance. In the algorithm, I am using a DFS search in a graph and in every step, I add a char and a String. I am not sure whether this is the bottleneck of my algorithm (probably…
Smajl
  • 7,555
  • 29
  • 108
  • 179
1
vote
1 answer

Most optimized way to set 63rd bit of an MMIO register on mips64

I need to set 63rd bit(MSB) of a 64-bit MMIO register in MIPS assembly language. Currently I am doing this ldi $2,0x8000000000123456 #This is the address of the register which i want to set 63 rd bit ld $3,0($2) # read current value from the…
Chinna
  • 3,930
  • 4
  • 25
  • 55
1
vote
3 answers

Least expensive equality search for small unsorted arrays

What is the most efficient method, in terms of execution time, to search a small array of about 4 to 16 elements, for an element that is equal to what you're searching for, in C++? The element being searched for is a pointer in this case, so it's…
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
1
vote
1 answer

Micro-optimization of IF statement

I always look into how to optimize the code more and more every day, and how I can learn to code faster code. Looking at some minified JS online I notice the change of a few IF into a statement and I thought that it might be a very good idea to…
Fabrizio
  • 3,734
  • 2
  • 29
  • 32
1
vote
2 answers

Preserving the Execution pipeline

Return types are frequently checked for errors. But, the code that will continue to execute may be specified in different ways. if(!ret) { doNoErrorCode(); } exit(1); or if(ret) { exit(1); } doNoErrorCode(); One way heavyweight CPU's can…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
1
vote
4 answers

C programming and error_code variable efficiency

Most code I have ever read uses a int for standard error handling (return values from functions and such). But I am wondering if there is any benefit to be had from using a uint_8 will a compiler -- read: most C compilers on most architectures --…
1
vote
2 answers

Multiply one fixed matrix by a huge number of vectors

I'll need to change the basis of some 10^7 vectors, each having 200 coordinates. So I will multiply one [200 x 200] matrix by 10^7 [200 x 1] vectors. I need it to run very fast but I need to code it fast (one day or less) and my CUDA is poor, so I…
Jorge
  • 742
  • 5
  • 8
1
vote
2 answers

Are there any performance benefits when using x += 1 instead of x = x+1?

I am confused whether or not it is advisable to use x += 1 or x = x+1. I know that they both produce the same results. In practical terms, are there any performance gain when using x+=1 instead of x = x+1? Will it make my program run faster?
Minelava
  • 221
  • 1
  • 7
  • 21
1
vote
4 answers

Which kind of quotation mark is more efficient?

Just curious, which is more efficient? This: String a = someString + "." + anotherString; Or this: String a = someString + '.' + anotherString;
aberrant80
  • 12,815
  • 8
  • 45
  • 68
1
vote
3 answers

How can I check which part of a method uses the most time?

I'm working on a project for optimizing an existing algorithm. I profiled the code with JIP and VisualVM and got a main bottleneck. Now I want to see what takes the most time IN the method. What is an easy way to do that? Are there profilers that…
Lonefish
  • 647
  • 2
  • 11
  • 32
1
vote
1 answer

while loop optimization a little bit

I've got a while loop running that grabs all the posts on my site while ( $all_query->have_posts() ) : $all_query->the_post(); there is meta data in each on that I need to play with. It's a field called 'rate' and I need to merge like values,…
Xhynk
  • 13,513
  • 8
  • 32
  • 69
1
vote
4 answers

Cheapest way to force implicit array bounds check

Let's say I have a method with the following signature: public int indexOf(byte[] bytes, byte toFind, int offset, int length) { ... } This method does something simple like look for the byte toFind in the range [offset, offset+length) in bytes. …
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
1
vote
4 answers

Placing index columns on the left of a mysql WHERE statement?

I was curious since i read it in a doc. Does writing select * from CONTACTS where id = ‘098’ and name like ‘Tom%’; speed up the query as oppose to select * from CONTACTS where name like ‘Tom%’ and id = ‘098’; The first has an indexed column on…
An employee
  • 6,188
  • 9
  • 33
  • 43
1
vote
6 answers

Java, Most Expensive Statements?

What are the most expensive (both in terms of bytecode and cpu cycles) statements in the Java Programming language?
Kevin Boyd
  • 12,121
  • 28
  • 86
  • 128