In computer science, program optimization or software optimization is the process of modifying a software system to make some aspect of it work moreefficiently or use fewer resources.In general, a computer program may be optimized so that it executes more rapidly, or is capable of operating with lessmemory storage or other resources, or draw less power. (wiki)
Levels of optimization
Design - architecture(network latency bounded, program language, platform, goal)
Algorithm and data structure - algorithm(constant,logarithmic,linear,log-linear; quadratic complexity O(n^2) fail to scale); data structure(use abstract data types in function definitions, restrict the concrete definition to a few places); memoization (cache the result of expensize function call)
source code level - for, while
build level - optimizing compile; preprocessor directive(for specific hardware)
assembly language - operating system on embedded systems writen in assembly language
Algorithm and data structure
algorithm(constant,logarithmic,linear,log-linear; quadratic complexity O(n^2) fail to scale);
data structure(use abstract data types in function definitions, restrict the concrete definition to a few places)
memoization (cache the result of expensize function call)
optimize the code
limitation of the compiler
1. alias of the storage address - two pointer points to the same address
2. side effect of subfunction - subfunction change the value of global variable
loop-invariant code motion - low efficiency of the for loop
as to some code in the for loop, the execution result remains the same with every iteration. it would be more efficient to move this code block out of the for loop
reduce unnecessary reference/read/access to the memory/storage/disk
if it is not the final value, it is better to store it to a temporary varaible. And, just write the final result once to the memory. This reduce the memory access.