Automated andmanual optimization
Optimizing a whole system is usually undertaken by programmers because it is too complex for automated optimizers. In this situation, programmers or system administrators explicitly change code so that the overall system performs better. Although it can produce better efficiency, it is far more expensive than automated optimizations. Since many parameters influence the program’s performance, the optimization space is large. Meta-heuristics and machine learning are used to address the complexity of program optimization.
When to optimize
Optimization can reduce readability and add code that is used only to improve performance. This
may complicate programs or systems, making them harder to maintain and debug. As a result,
optimization or performance tuning is often performed at the end of the development stage.
Levels of optimization
At the highest level, the design may be optimized to make the best use of the available resources, given goals, constraints, and expected use and load. The architectural design of a system overwhelmingly affects its performance.
Algorithms and data structures
Given an overall design, a good choice of efficient algorithms and data structures and an efficient implementation of these algorithms and data structures come next. After design, the choice of algorithms and data structures affects efficiency more than any other aspect of the program.
Source code level
Beyond general algorithms and their implementation on an abstract machine, concrete source code-level choices can make a significant difference. This depends on the source language, the target machine language, and the compiler and can be both difficult to understand or predict and change over time; this is a key place where understanding compilers and machine code can improve performance.
Between the source and compile levels, directives and build flags can be used to tune performance options in the source code and compiler, respectively, such as using preprocessor defines to disable unneeded software features, optimizing for specific processor models or hardware capabilities, or predicting branching, for instance.
At the lowest level, writing code using an assembly language designed for a particular hardware platform can produce the most efficient and compact code if the programmer takes advantage of the full repertoire of machine instructions. For this reason, many operating systems used on embedded systems have traditionally been written in assembler code.