The use of functions

The use of functions provides many benefits, including:

  • The code inside the function can be reused.
  • It is much easier to change or update the code in a function (which needs to be done once) than for every in-place instance. Duplicate code is a recipe for disaster.
  • It makes your code easier to read and understand, as you do not have to know how a function is implemented to understand what it does.
  • The function provides type checking.

However, one major downside of functions is that every time a function is called, there is a certain amount of performance overhead that occurs. This is because the CPU must store the address of the current instruction it is executing (so it knows where to return to later) along with other registers, all the function parameters must be created and assigned values, and the program has to branch to a new location. Code written in-place is significantly faster.

For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run. However, for small, commonly-used functions, the time needed to make the function call is often a lot more than the time needed to actually execute the function’s code. This can result in a substantial performance penalty.

C++ offers a way to combine the advantages of functions with the speed of code written in-place: inline functions. The inline keyword is used to request that the compiler treat your function as an inline function. When the compiler compiles your code, all inline functions are expanded in-place — that is, the function call is replaced with a copy of the contents of the function itself, which removes the function call overhead! The downside is that because the inline function is expanded in-place for everyfunction call, this can make your compiled code quite a bit larger, especially if the inline function is long and/or there are many calls to the inline function.

Consider the following snippet:

1
2
3
4
5
6
7
8
9
10
11
12
int min( int nX, int nY)
{
     return nX > nY ? nY : nX;
}
 
int main()
{
     using namespace std;
     cout << min(5, 6) << endl;
     cout << min(3, 2) << endl;
     return 0;
}

This program calls function min() twice, incurring the function call overhead penalty twice. Because min() is such a short function, it is the perfect candidate for inlining:

1
2
3
4
inline int min( int nX, int nY)
{
     return nX > nY ? nY : nX;
}

Now when the program compiles main(), it will create machine code as if main() had been written like this:

1
2
3
4
5
6
7
int main()
{
     using namespace std;
     cout << (5 > 6 ? 6 : 5) << endl;
     cout << (3 > 2 ? 2 : 3) << endl;
     return 0;
}

This will execute quite a bit faster, at the cost of the compiled code being slightly larger.

Because of the potential for code bloat, inlining a function is best suited to short functions (eg. no more than a few lines) that are typically called inside loops and do not branch. Also note that the inline key word is only a recommendation — the compiler is free to ignore your request to inline a function. This is likely to be the result if you try to inline a lengthy function!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值