 | Optimization computer science: Encyclopedia II - Optimization computer science - Automated and manual optimization
Optimization computer science - Automated and manual optimization
Optimization can be automated by compilers or performed by programmers. Gains are usually limited for local optimization, and larger for global optimizations. Usually, the most powerful optimization is to find a superior algorithm.
Optimizing a whole system is usually done by human beings because the system is too complex for automated optimizers. Grid computing or distributed computing aims to optimize the whole system, by moving tasks from computers with high usage to computers with idle time.
In this technique, programmers or system administrators explicitly change code so that the system performs better. Although it can produce better efficiency, it is far more expensive than automated optimizations.
First of all, it is extremely important to use a profiler to locate the bottleneck. Programmers usually think they have a clear idea of what the bottleneck is, but they are frequently completely wrong. Optimizing an unimportant piece of code will not help the overall program speed.
When the bottleneck is localized, optimization usually starts with a rethinking of the algorithm used in the program: more often than not, a particular algorithm can be specifically tailored to a particular problem, yielding better performance than a generic algorithm. For example, the task of sorting a huge list of items is usually done with a quicksort routine, which is one of the most efficient generic algorithms. But if some characteristic of the items is exploitable (for example, they are already arranged in some particular order), a different method can be used, or even a custom-made sort routine.
After one is reasonably sure that the best algorithm is selected, code optimization can start: loops can be unrolled (for lower loop overhead, although this can often lead to lower speed, due to overloading the processor's instruction cache), data types as small as possible can be used, integer arithmetic can be used instead of a floating-point one, and so on.
Performance bottlenecks can be due to the language rather than algorithms or data structures used in the program. Sometimes, a critical part of the program can be re-written in a different, faster programming language. For example, it is common for very high-level languages like Python to have modules written in C for greater speed. Programs already written in C can have modules written in assembly. Programs written in D can use the inline assembler.
Rewriting pays off because of a law known as the 90/10 law, which states that 90% of the time is spent in 10% of the code, and only 10% of the time in the remaining 90% of the code. So optimizing just a small part of the program can have a huge effect on the overall speed.
Manual optimization often has the side-effect of undermining readability. Thus code optimizations should be carefully documented and their effect on future development evaluated.
The program that does the automated optimization is called an optimizer. Most optimizers are embedded in compilers and operate during compilation. Optimizers often can tailor the generated code to specific processors.
Today, automated optimizations are almost exclusively limited to compiler optimization.
Some high-level languages (Eiffel, Esterel) optimize their programs by using an intermediate language.
Other related archives90/10 law, Abstract interpretation, C, Caching, Compiler optimization, Control flow graph, D, Donald Knuth, Eiffel, Esterel, FX!32, Grid computing, Internet, JavaScript, Lazy evaluation, Load balancing, Low level virtual machine, Memoization, Memory locality, Optimization of Java, PHP, Pareto principle, Performance analysis, Python, Queueing theory, Rob Pike, SSA form, Simulation, Speculative execution, Tony Hoare, Worst case execution time, algorithm, algorithms, anti-pattern, assembly, bottleneck, cache, compiler bugs, compiler optimization, computer architecture, computer program, computer programming, computers, computing, debug, development stage, distributed computing, efficiency, high-level, inline assembler, instruction cache, intermediate language, just-in-time compilers, layer 4 router, maintainability, operations research, performance, profiler, programming language, prototype, quicksort, readability, settings, tradeoff, tradeoffs, transparently, whitespace
 Adapted from the Wikipedia article "Automated and manual optimization", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki |