内联函数C ++ [英] Inline functions C++

查看:71
本文介绍了内联函数C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内联函数和函数原型之间有什么区别.
使用内联函数的优点是什么?它的作用是什么?

What is the difference between inline funtions and function prototypes.
what is the advantage of using inline funtions?What is its role?

推荐答案

谷歌搜索的前两个链接:
http://en.wikipedia.org/wiki/Inline_function [ http://www.parashift.com/c++-faq/inline-functions.html [ ^ ]
First two links on a google search:
http://en.wikipedia.org/wiki/Inline_function[^]
http://www.parashift.com/c++-faq/inline-functions.html[^]


内联函数

内联函数是编译器使用的优化技术.只需在函数原型前添加inline关键字即可使函数内联.内联函数指示编译器在代码中使用该函数的位置插入该函数的完整主体.

优点:-
1)不需要函数调用开销.
2)在函数调用时,还节省了堆栈中变量push/pop的开销.
3)它还节省了从函数返回调用的开销.
4)通过利用指令缓存来增加引用的局部性.
5)内联之后,如果指定,编译器还可以应用过程内优化.这是最重要的一个,通过这种方式,编译器现在可以专注于消除死代码,可以更加注重分支预测,归纳变量消除等.

缺点:-
1)可能会增加函数的大小,使其可能无法容纳在缓存中,从而导致很多cahce miss.
2)内联函数之后,如果要使用寄存器的变量号增加的数量超过了它们的数量,则可能会增加寄存器变量资源利用的开销.
3)这可能会导致编译开销,就好像某些主体更改了内联函数中的代码而不是所有调用位置一样.
4)如果在头文件中使用,它将使您的头文件变大,并可能使其不可读.
5)如果有人使用了太多的内联函数,导致代码大小大于可能导致内存崩溃的情况.越来越多的页面错误降低了程序性能.
6)对于由于内存大小限制而根本不希望使用大二进制大小的嵌入式系统没有用.

性能:-
现在涵盖了大多数人对性能"感兴趣的主题.
在大多数情况下,如果谨慎使用内联函数可以提高性能,因为它可以节省大量开销,如上面优势"部分中所述,但是由于我们也已经讨论了其劣势,因此在使用它们时需要非常谨慎.当今的现代编译器会自动内联函数,因此在大多数情况下无需明确指定.尽管放置inline关键字仅使编译器暗示可以通过内联来优化此功能,但最终还是由编译器决定使其内联.尽管也有一些方法可以指示编译器,但像使用Microsoft的Visual c ++一样使用__forceinline来使函数内联,可以使用__forceinline来指示编译器内联函数.我建议不要使用此关键字,除非您非常确定性能会有所提高.使函数内联可能不会提高您的性能,这也完全取决于您的代码流.不要期望在代码的函数之前添加内联关键字,从而获得神奇的性能提升,因为当今大多数编译器都会自动这样做.
功能原型
有关其他用途,请参见原型(计算机科学).
用C,Perl或C ++编写的函数原型是对函数的声明,该声明省略了函数主体,但确实指定了函数的返回类型,名称,参数和参数类型.虽然函数定义指定了函数的功能,但可以将函数原型视为指定其接口.
在原型中,参数名称是可选的,但是类型必须与所有修饰符一起使用(例如,如果它是指针或const参数).
inline function

Inline function is the optimization technique used by the compilers. One can simply prepend inline keyword to function prototype to make a function inline. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.

Advantages :-
1) It does not require function calling overhead.
2) It also save overhead of variables push/pop on the stack, while function calling.
3) It also save overhead of return call from a function.
4) It increases locality of reference by utilizing instruction cache.
5) After in-lining compiler can also apply intraprocedural optmization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..

Disadvantages :-
1) May increase function size so that it may not fit on the cache, causing lots of cahce miss.
2) After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization.
3) It may cause compilation overhead as if some body changes code inside inline function than all calling location will also be compiled.
4) If used in header file, it will make your header file size large and may also make it unreadable.
5) If somebody used too many inline function resultant in a larger code size than it may cause thrashing in memory. More and more number of page fault bringing down your program performance.
6) Its not useful for embeded system where large binary size is not preferred at all due to memory size constraints.

Performance : -
Now covering the topic which most the people are interested in the "Performance".
In most of the cases Inline function boost performance if used cautiously as it saves lots of overhead as discussed in our Advantages section above but as we have also discussed its disadvantages one need to be very cautious while using them. Today''s modern compiler inline functions automatically, so no need to specify explicitly in most of the cases. Although placing inline keyword only gives compiler a hint that this function can be optimized by doing in-lining, its ultimately compiler decision to make it inline. Though there are ways to instruct compiler too, for making a function call inline like one can use __forceinline to instruct compiler to inline a function while working with microsoft visual c++. I suggest not to use this keyword until you are very sure about performance gain. Making a function inline may or may not give you performance boost, it all depends on your code flows too. Don''t expect a magical performance boost by prepending inline keyword before a function to your code as most of the compiler nowadays does that automatically.
Function prototype
For other uses, see Prototype (computer science).
A function prototype in C, Perl or C++ is a declaration of a function that omits the function body but does specify the function return type, name, arity and argument types. While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.
In a prototype, argument names are optional, however, the type is necessary along with all modifiers (e.g. if it is a pointer or a const argument).


这篇关于内联函数C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆