Optimizing C++/Code optimization/Run-time support

C++ run-time support routines obviously have a significant cost, because otherwise such behavior would have be inlined. Here techniques are presented to avoid the language features that cause an implicit call to costly run-time support routines.

The operator
Instead of using the  operator, use a   function.

Such operator may take more time than a virtual function call.

The operator
Instead of the  operator, use the   operator, or, better, a   function call.

Such operator may take a time noticeably longer than a virtual function call, and longer also than the  operator.

Empty exception specification
Use the empty exception specification (that is, append  to the declaration) for the functions you are sure will never throw exceptions.

 In C++11 use  instead.

Some compilers use such information to simplify the bookkeeping needed to handle exceptions.

The statement
For every bottleneck, move before the bottleneck the  keywords, and after the bottleneck the matching   clauses.

In other words, hoist  statements out of bottlenecks.

The execution of a  statement sometimes is free of charge, but other times causes as a slowdown. Avoid the repeated execution of such block inside bottlenecks.

Floating point vs integer operations
If the target processor does not contain a floating point unit, replace floating point functions, constants and variables with the corresponding integer functions, constants and variables; if the target processor contains only a single precision floating point unit, replace  functions, constants and variables with their   correspondents.

Present processors for desktop or server computers contain dedicated hardware for floating point arithmetic, both at single and at double precision, and therefore such operations are almost as fast as their integer correspondents.

Instead, some processors for embedded systems do not contain dedicated hardware for floating point arithmetic, or contain hardware able to handle only single precision numbers. Therefore, in such systems, the operation that cannot be performed by hardware are emulated by very slow library functions. In such case, it is much more efficient to use integer arithmetic, or, if available in hardware, single precision floating point arithmetic.

To handle fractional numbers by using integer operations, every number is to be meant as multiplied by a scale factor. To do that, every number is multiplied by such factor at input, and is divided by the same factor at output, or vice versa.

Number to string conversion
Use optimized functions to convert numbers to strings.

The standard functions to convert an integer number to a string or a floating point number to string are rather inefficient. To speed up such operations, use non-standard optimized function, possibly written by yourself.

Use of functions
To perform input/output operations, instead of using the C++ streams, use the old C functions, declared in the  header.

C++ I/O primitives have been designed mainly for type safety and for customization rather than for performance, and many library implementation of them turn out to be rather inefficient. In particular, the C language I/O functions  and   are more efficient than the     and   member functions.

If you have to use C++ streams, use  instead of   since   also flushes the stream.

Ottimizzare C++/Ottimizzazione del codice C++/Supporto run-time