pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

什么時候使用內(nèi)聯(lián)函數(shù),什么時候不使用?

When to use inline function and when not to use it?(什么時候使用內(nèi)聯(lián)函數(shù),什么時候不使用?)
本文介紹了什么時候使用內(nèi)聯(lián)函數(shù),什么時候不使用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我知道內(nèi)聯(lián)是對編譯器的提示或請求,用于避免函數(shù)調(diào)用開銷.

那么根據(jù)什么可以確定一個函數(shù)是否適合內(nèi)聯(lián)呢?在哪種情況下應(yīng)該避免內(nèi)聯(lián)?

解決方案

避免函數(shù)調(diào)用的成本只是故事的一半.

做:

  • 使用 inline 而不是 #define
  • 非常小的函數(shù)非常適合內(nèi)聯(lián):更快的代碼和更小的可執(zhí)行文件(更多機會留在代碼緩存中)
  • 這個函數(shù)很小并且經(jīng)常被調(diào)用

不要:

  • 大型函數(shù):導(dǎo)致更大的可執(zhí)行文件,這會顯著影響性能,而不管調(diào)用開銷導(dǎo)致執(zhí)行速度更快
  • I/O 綁定的內(nèi)聯(lián)函數(shù)
  • 很少使用該函數(shù)
  • 構(gòu)造函數(shù)和析構(gòu)函數(shù):即使為空,編譯器也會為它們生成代碼
  • 在開發(fā)庫時破壞二進制兼容性:
    • 內(nèi)聯(lián)現(xiàn)有函數(shù)
    • 更改內(nèi)聯(lián)函數(shù)或使內(nèi)聯(lián)函數(shù)成為非內(nèi)聯(lián)函數(shù):庫的先前版本調(diào)用舊實現(xiàn)

在開發(fā)庫時,為了使類在未來可擴展,您應(yīng)該:

  • 即使主體為空也添加非內(nèi)聯(lián)虛擬析構(gòu)函數(shù)
  • 使所有構(gòu)造函數(shù)都非內(nèi)聯(lián)
  • 編寫復(fù)制構(gòu)造函數(shù)和賦值運算符的非內(nèi)聯(lián)實現(xiàn),除非類不能按值復(fù)制

請記住,inline 關(guān)鍵字是對編譯器的一個提示:編譯器可能決定不內(nèi)聯(lián)函數(shù),并且可以決定內(nèi)聯(lián)未標(biāo)記為 inline 的函數(shù)首先.我通常避免標(biāo)記函數(shù) inline (除了編寫非常非常小的函數(shù)時).

關(guān)于性能,明智的方法是(一如既往)分析應(yīng)用程序,然后最終內(nèi)聯(lián)一組表示瓶頸的函數(shù).

參考文獻:

  • 內(nèi)聯(lián)或不內(nèi)聯(lián)
  • [9] 內(nèi)聯(lián)函數(shù)
  • C++ 的政策/二進制兼容性問題
  • GotW #33:內(nèi)聯(lián)
  • 內(nèi)聯(lián) Redux
  • 有效的 C++ - 第 33 條:明智地使用內(nèi)聯(lián)
<小時>

Bjarne Stroustrup,C++ 編程語言:

<塊引用>

一個函數(shù)可以被定義為inline.例如:

inline int fac(int n){返回 (n <2) ?1 : n * fac(n-1);}

<塊引用>

inline 說明符提示編譯器它應(yīng)該嘗試為內(nèi)聯(lián)的 fac() 調(diào)用生成代碼,而不是為函數(shù)放置代碼一次然后通過通常的函數(shù)調(diào)用機制調(diào)用.聰明的編譯器可以為調(diào)用 fac(6) 生成常量 720.內(nèi)聯(lián)函數(shù)相互遞歸的可能性,內(nèi)聯(lián)函數(shù)是否遞歸取決于輸入等,使得無法保證inline函數(shù)的每次調(diào)用實際上都是內(nèi)聯(lián)的.編譯器的聰明程度無法被立法,因此一個編譯器可能生成720,另一個6 * fac(5),還有另一個未內(nèi)聯(lián)調(diào)用fac(6).

為了在沒有異常聰明的編譯和鏈接工具的情況下使內(nèi)聯(lián)成為可能,內(nèi)聯(lián)函數(shù)的定義——而不僅僅是聲明——必須在范圍內(nèi)(第 9.2 節(jié)).inline 說明符不會影響函數(shù)的語義.特別是,內(nèi)聯(lián)函數(shù)仍然具有唯一地址,因此具有內(nèi)聯(lián)函數(shù)的 static 變量(第 7.1.2 節(jié)).

ISO-IEC 14882-1998,7.1.2 功能說明符

<塊引用>

帶有 inline 說明符的函數(shù)聲明 (8.3.5, 9.3, 11.4) 聲明了一個內(nèi)聯(lián)函數(shù).內(nèi)聯(lián)說明符向?qū)崿F(xiàn)表明,在調(diào)用點對函數(shù)體進行內(nèi)聯(lián)替換優(yōu)于通常的函數(shù)調(diào)用機制.不需要實現(xiàn)在調(diào)用點執(zhí)行此內(nèi)聯(lián)替換;但是,即使省略了這種內(nèi)聯(lián)替換,7.1.2 中定義的內(nèi)聯(lián)函數(shù)的其他規(guī)則仍應(yīng)遵守.

I know that inline is a hint or request to compiler and its used to avoid function call overheads.

So on what basis one can determine whether a function is a candidate for inlining or not ? In which case one should avoid inlining ?

解決方案

Avoiding the cost of a function call is only half the story.

do:

  • use inline instead of #define
  • very small functions are good candidates for inline: faster code and smaller executables (more chances to stay in the code cache)
  • the function is small and called very often

don't:

  • large functions: leads to larger executables, which significantly impairs performance regardless of the faster execution that results from the calling overhead
  • inline functions that are I/O bound
  • the function is seldom used
  • constructors and destructors: even when empty, the compiler generates code for them
  • breaking binary compatibility when developing libraries:
    • inline an existing function
    • change an inline function or make an inline function non-inline: prior version of the library call the old implementation

when developing a library, in order to make a class extensible in the future you should:

  • add non-inline virtual destructor even if the body is empty
  • make all constructors non-inline
  • write non-inline implementations of the copy constructor and assignment operator unless the class cannot be copied by value

Remember that the inline keyword is a hint to the compiler: the compiler may decide not to inline a function and it can decide to inline functions that were not marked inline in the first place. I generally avoid marking function inline (apart maybe when writing very very small functions).

About performance, the wise approach is (as always) to profile the application, then eventually inline a set of functions representing a bottleneck.

References:

  • To Inline or Not To Inline
  • [9] Inline functions
  • Policies/Binary Compatibility Issues With C++
  • GotW #33: Inline
  • Inline Redux
  • Effective C++ - Item 33: Use inlining judiciously

EDIT: Bjarne Stroustrup, The C++ Programming Language:

A function can be defined to be inline. For example:

inline int fac(int n)
{
  return (n < 2) ? 1 : n * fac(n-1);
}

The inline specifier is a hint to the compiler that it should attempt to generate code for a call of fac() inline rather than laying down the code for the function once and then calling through the usual function call mechanism. A clever compiler can generate the constant 720 for a call fac(6). The possibility of mutually recursive inline functions, inline functions that recurse or not depending on input, etc., makes it impossible to guarantee that every call of an inline function is actually inlined. The degree of cleverness of a compiler cannot be legislated, so one compiler might generate 720, another 6 * fac(5), and yet another an un-inlined call fac(6).

To make inlining possible in the absence of unusually clever compilation and linking facilities, the definition–and not just the declaration–of an inline function must be in scope (§9.2). An inline especifier does not affect the semantics of a function. In particular, an inline function still has a unique address and so has static variables (§7.1.2) of an inline function.

EDIT2: ISO-IEC 14882-1998, 7.1.2 Function specifiers

A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.

這篇關(guān)于什么時候使用內(nèi)聯(lián)函數(shù),什么時候不使用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
C99 stdint.h header and MS Visual Studio(C99 stdint.h 頭文件和 MS Visual Studio)
主站蜘蛛池模板: 爱科技iMobile-专业的科技资讯信息分享网站 | 垃圾清运公司_环卫保洁公司_市政道路保洁公司-华富环境 | 舞台木地板厂家_体育运动木地板_室内篮球馆木地板_实木运动地板厂家_欧氏篮球地板推荐 | pH污水传感器电极,溶解氧电极传感器-上海科蓝仪表科技有限公司 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 低压载波电能表-单相导轨式电能表-华邦电力科技股份有限公司-智能物联网综合管理平台 | 短信通106短信接口验证码接口群发平台_国际短信接口验证码接口群发平台-速度网络有限公司 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 福建成考网-福建成人高考网 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 喷涂流水线,涂装流水线,喷漆流水线-山东天意设备科技有限公司 | 成都治疗尖锐湿疣比较好的医院-成都治疗尖锐湿疣那家医院好-成都西南皮肤病医院 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 粘度计,数显粘度计,指针旋转粘度计 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 雷达液位计_超声波风速风向仪_雨量传感器_辐射传感器-山东风途物联网 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 深圳南财多媒体有限公司介绍| 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 北京浩云律师事务所-法律顾问_企业法务_律师顾问_公司顾问 | 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 底部填充胶_电子封装胶_芯片封装胶_芯片底部填充胶厂家-东莞汉思新材料 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 磁力加热搅拌器-多工位|大功率|数显恒温磁力搅拌器-司乐仪器官网 | 数控走心机-双主轴走心机厂家-南京建克 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | SPC工作站-连杆综合检具-表盘气动量仪-内孔缺陷检测仪-杭州朗多检测仪器有限公司 | 盐城网络公司_盐城网站优化_盐城网站建设_盐城市启晨网络科技有限公司 | 湖南教师资格网-湖南教师资格证考试网 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 刚性-柔性防水套管-橡胶伸缩接头-波纹管补偿器-启腾供水材料有限公司 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 汽车水泵_汽车水泵厂家-瑞安市骏迪汽车配件有限公司 | 双舌接地线-PC68数字式高阻计-ZC36|苏海百科|