03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

40 CHAPTER 2. <strong>C++</strong> BASICS<br />

2.6.1 Inline Functions<br />

Calling a function requires a fair amount of activities:<br />

• The arguments (or at least their addresses) must be copied on the stack;<br />

• The current program counter must be copied on the stack to continue the execution at<br />

this point when the function is finished;<br />

• Save registers to allow the function using them;<br />

• Jump to the code of the function;<br />

• Execute the function;<br />

• Clean the arguments from the stack;<br />

• Copy the result on the stack;<br />

• Jump back to the calling code;<br />

• Store back registers.<br />

What happens exactly depends on the hardware. The good news is that the function call<br />

overhead is dramatically lower than in the past. Furthermore, the compiler can optimize out<br />

those activities not needed in a specific call.<br />

Nonetheless, <strong>for</strong> small functions, like the square above, the ef<strong>for</strong>t <strong>for</strong> calling the function is still<br />

significantly higher than what the function actually does. C programmers avoid the function-call<br />

overhead by macros. Macros create so many problems in the software development that they<br />

must only be used when there is absolutely no alternative whatsoever. Bjarne Stroustrup<br />

says “Almost every macro demonstrates a flaw in the programming language, in the program,<br />

or in the programmer.” We like to add a flaw “in the compiler optimization”. 16<br />

Fortunately, we have an excellent alternative to macros: inline functions. The programmer just<br />

adds the keyword inline to the function definition:<br />

inline double square(double x)<br />

{<br />

return x ∗ x;<br />

}<br />

and all the overhead of the function call vanishes into thin air.<br />

An excessive use of inline can have a negative effect on per<strong>for</strong>mance. When many large functions<br />

are inlined then the binary executable becomes very large. The consequence is that a lot of<br />

time is spend loading the binary from memory and lots of cache memory is wasted <strong>for</strong> it as<br />

well. This decreases the memory bandwidth and cache available <strong>for</strong> data, causing more slow<br />

down than what is saved on function calls.<br />

16 Advanced: Compilers are today really smart in eliminating unused code. However, we experienced that<br />

arguments of inline functions might be constructed although they are not used. This are usually only few<br />

machine instructions. But when this happens extremely frequently as in an index range check that should<br />

disappear in release mode, it can ruin the overall per<strong>for</strong>mance. We hope that further compiler improvement can<br />

rescue us from this kind of macro usage.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!