Your question is one of the questions that has no answer that could be called "absolute truth." The overhead of calling a normal function depends on three factors:
CPU. The overhead of x86, PPC, and ARM processors is very different, and even if you just stay with the same architecture, the overhead also varies greatly between Intel Pentium 4, Intel Core 2 Duo, and Intel Core i7. Overhead can even change markedly between Intel and AMD processors, even if they run at the same clock speed, since factors such as cache sizes, cache algorithms, memory access patterns, and the actual hardware implementation of the call code itself can have a huge impact on overhead .
ABI (Application Binary Interface). Even with the same CPU, different ABIs often exist that determine how the function calls the pass parameters (through registers, through the stack, or through a combination of both), and also where and how the freeze frame is initialized and cleared. All this affects overhead. Different operating systems may use different ABIs for the same CPU; for example, Linux, Windows, and Solaris can use three different ABIs for the same CPU.
Compiler. Strict compliance with ABI is only important if a function is called between independent blocks of code, for example. if the application calls the function of the system library or the user library calls the function of another user library. As long as the "private" functions are not visible outside a specific library or binary code, the compiler can "trick" it. It cannot strictly follow the ABI, but instead use shortcuts that lead to faster function calls. For example. it can pass parameters in the register instead of using the stack, or it can skip the setup of the stack frame and completely clear it if it really is not necessary.
If you want to know the overhead for a particular combination of the three factors mentioned above, for example, for Intel Core i5 on Linux using GCC, your only way to get this information is to compare the differences between the two implementations, one using function calls and where you copy the code directly to the caller; thus, you force the inlay into place, since the inline operator is just a hint and does not always lead to inlining.
However, the real question here is: is overhead really important? One thing is for sure: a function call always has overhead. It may be small, it may be large, but it certainly exists. And no matter how small this function is, if the function is called often enough in a performance-critical section, the overhead will to some extent matter. Inlining rarely makes your code slower if you are not terribly overdoing it; this will make the code bigger. Today, compilers do pretty well when they are built in and when not, so you are unlikely to ever need your brain.
Personally, I completely ignore inlining during development, until I have a more or less usable product that I can profile, and only if the profile tells me that a certain function is called very often, as well as in the critical section of the application, then I will consider "forced attachment" of this function.
So far my answer is very general, it applies to C as much as it does for C ++ and Objective-C. As a closing word, let me say something about C ++ in particular: virtual methods are calls with a double indirect function, which means that they have a higher overhead on functions than regular function calls, and also cannot be built in. Non-virtual methods can be built-in by the compiler or not, but even if they are not built-in, they are still significantly faster than virtual ones, so you should not create methods virtual unless you plan to redefine or redefine them.
Mecki Mar 03 2018-12-12T00: 00Z
source share