Understanding C++/Optimization

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Optimization[edit | edit source]

Definition:
Optimization is the process of fine-tuning the end results of an executable program to maximize efficiency or to minimize resource usage.

Modern compilers generally have settings to automate the most common optimizations during the compiling process, often greatly reducing the number of optimizations that programmers might need to consider. Modern compilers commonly take advantage of knowledge about a specific system and architecture in the optimization process, as well as years of optimization research results, to further improve performance and minimize resource usage. Such optimizations are best left to the compiler since bloat or performance costs could result when done by hand.

When faced with a problem you try to find solutions that solve the problem. Sometimes the solution is straightforward and at other times the possible solutions aren't as straightforward and may require planning and deciding what steps to take. Algorithms are the detailed steps which form the solution to a problem for programmers. Algorithm research continue to find simpler and faster ways to solve problems, reconsider and reduce problems, and divide problems into easier to manage problems. Optimization is very important in algorithmic programming, but not all algorithms may benefit from optimization.

Problems like searching for names in a list or sorting them are solved with algorithms. Sorting algorithms for example, have been developed since the 1950s and new solutions are still being found. The choice of what algorithm to use depends on what is more important time or space. If it is more important that a program be fast than conserve memory usage, often values can be stored ahead of time that will allow for less computation later on. Similarly, an algorithm can use far less space if it is given the time to re-compute all values.

Often, a program can be optimized by simply realizing that it is doing more than it has to. As there are many ways of solving problems, one solution may work but be grossly inefficient when compared with other methods, as was the case with the first sorting algorithm, the bubble sort. Problems that involve searching through lists or binary trees may be exponentially slower than they need to be if a naive approach is used. If a part of the program is too slow to perform reliably, researching the problem at hand for a faster method may be worthwhile.

Redundancies[edit | edit source]

Redundancies can increase the size and time needed to solve problems. Modern compilers can usually eliminate some redundancies, such as, variables, functions, values and statements never used and constant variables, values and computational results. However, the complexity of algorithms can reduce the effectiveness of eliminating redundancies by the optimization process of compilers.

  • Example of redundancies
 int foo = 0, bar = 2+2*4, baz = 9;
 
 if (foo) {
   while (foo) {
     ...
   }
 }
 
 if (bar) {
   do_loop(bar);
 }
 
 void do_loop(int bar) {
   while (bar) {
     ...
   }
   return;
   do_nothing();
 }

Common strategy for programmers to decrease or eliminate redundancies:

  • when creating small functions assume a valid state.
  • limit validating tests to events outside your control such as user input, computer resources and external functions.

Code reuse[edit | edit source]

Optimization is also reflected on the effectiveness of a code. If you can use an already existing code base/framework that a considerable number of programmers have access to, you can expect it to be less buggy and optimized to solve your particular need.

Some of these code repositories are available to C++ programmers as libraries. Be careful to consider dependencies and check how implementation is done: if used without considerations this can also lead to code bloat and increased memory footprint, as well as decrease the portability of the code. We will take a close look at some of them in the Standard Libraries section of the book.

Further reading[edit | edit source]