Micro optimization - Falmouth-Games-Academy/comp350-research-journal GitHub Wiki

Micro-Optimisation

Micro Optimisations are 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.) 1(https://www.codeproject.com/Articles/825636/The-Importance-of-Useless-Micro-optimization).

This process of optimizing small sections to reduce the number of operations performed by the CPU is often a topic of much debate. For example in this thread 2(https://stackoverflow.com/questions/3470990/is-micro-optimization-worth-the-time) which gives an example:

// Original 
is_array($array)
// Optimisation
$array === (array) $array

The user also asks when micro-optimisations are worthwhile. The top answer argues that this type of optimisation is only worthwhile when there is evidence of a performance bottleneck. However the next user argues that the example optimisation is over 7 times faster than the original line, however, this is only 0.000001 seconds faster. Therefore this type of micro-optimisation is only worthwhile doing if the example line of code is run hundreds of thousands of times.

From these threads, we can conclude that optimisations that save small fractions of seconds are generally not useful for most applications that are run independently on single machines. This is because the time that will be saved will not likely be noticed by individual users and won't add enough to the quality of the application to justify the time taken by programmers to make these optimisations.

However, in applications where small sections of code can be run hundreds of thousands of times in a second, such as a web-server, these optimisations can really add up and save a lot of time. This will then, in turn, free up more time for the processor to perform more operations. In the context of a web-server, this is important as the server will be able to send more data to more users each second.

Floating point arithmetic

Micro-optimisations can also help compensate for floating point number evaluation shortfalls. In particular, by decomposing the operations into their constituent integer micro-operations, eliminating redundant shift operations, blocking together common computations, and scheduling the entire operation for better hardware utilization 3(https://apps.dtic.mil/dtic/tr/fulltext/u2/a202001.pdf).

In all cases of micro-optimisation the advantages of such incidental improvements must be balanced against the cost and time taken to implement them 4(http://delivery.acm.org/10.1145/1520000/1513451/v10i3_hyde.pdf?ip=86.153.15.167&id=1513451&acc=OPEN&key=4D4702B0C3E38B35%2E4D4702B0C3E38B35%2E4D4702B0C3E38B35%2E6D218144511F3437&__acm__=1549741127_e97f90b01ccedc499fdb43bdc6bf2768).

References

[1] https://www.codeproject.com/Articles/825636/The-Importance-of-Useless-Micro-optimization

[2] https://stackoverflow.com/questions/3470990/is-micro-optimization-worth-the-time

[3] Dally, William J. Micro-optimization of floating-point operations. Vol. 17. No. 2. ACM, 1989.

[4] Hyde, Randall. "The fallacy of premature optimization." Ubiquity 2009.February (2009): 1.