program/code optimization

来源:互联网 发布:bilibili mac版下载 编辑:程序博客网 时间:2024/06/17 01:50

In computer scienceprogram 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.

0 0
原创粉丝点击