Google C++ 编程规范 1.3

本文详细介绍了内联函数的定义、优势、劣势及决策准则。包括如何判断函数是否适合内联,以及避免过度内联导致的性能下降。特别强调了对析构函数和包含循环或判断语句的函数的处理。

Inline Functions

内联函数

▽Define functions inline only when they are small, say, 10 lines or less.

只有函数非常小,10行以内才定义为内联。

Definition:  You can declare functions in a way that allows the compiler to expand them inline rather than calling them through the usual function call mechanism.

定义:一种函数调用机制,你将函数声明为内联,编译器会将整个函数体在调用处展开。

Pros:  Inlining a function can generate more efficient object code, as long as the inlined function is small. Feel free to inline accessors and mutators, and other short, performance-critical functions.

前题条件:当内联一个很小函数时会产生更有效率的代码,考虑类成员访问函数或其他短小,性能要求严格的函数。

Cons:  Overuse of inlining can actually make programs slower. Depending on a function's size, inlining it can cause the code size to increase or decrease. Inlining a very small accessor function will usually decrease code size while inlining a very large function can dramatically increase code size. On modern processors smaller code usually runs faster due to better use of the instruction cache.

限制:过度使用内联函数实际上会使减慢程序运行,取决于函数体大小,它决定了代码增加或减少。内联一个非常小成员访问函数会减少代码,而内联一个非常大的函数代码会戏剧性的增长。在现代处理器上由于广泛使用指令缓存,小代码通常会运行得更快。

Decision:

决策: 

A decent rule of thumb is to not inline a function if it is more than 10 lines long. Beware of destructors, which are often longer than they appear because of implicit member- and base-destructor calls!

一个合适的经验法则:不要内联一个超过10行函数,但要小心析构函数,它并没有表面看上去那么小,因为它会调用类成员和基类的析构函数。

Another useful rule of thumb: it's typically not cost effective to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed).

其他有用的经验法则:一个函数如果有判断或循环语句通常是没效率的。(除非在通常的情况下循环语句或判断语句不会执行)。

It is important to know that functions are not always inlined even if they are declared as such; for example, virtual and recursive functions are not normally inlined. Usually recursive functions should not be inline. The main reason for making a virtual function inline is to place its definition in the class, either for convenience or to document its behavior, e.g., for accessors and mutators.

知道哪些函数即使你声明了内联也不会内联很重要:比如说虚函数和递归函数在一般情况下不会内联。通常情况下递归函数不应内联。使虚函数内联的主要原因在于类定义,主要是方便的原因,比如说成员访问函数。





(SCI三维路径规划对比)25年最新五种智能算法优化解决无人机路径巡检三维路径规划对比(灰雁算法真菌算法吕佩尔狐阳光生长研究(Matlab代码实现)内容概要:本文档主要介绍了一项关于无人机三维路径巡检规划的研究,通过对比2025年最新的五种智能优化算法(包括灰雁算法、真菌算法、吕佩尔狐算法、阳光生长算法等),在复杂三维环境中优化无人机巡检路径的技术方案。所有算法均通过Matlab代码实现,并重点围绕路径安全性、效率、能耗和避障能力进行性能对比分析,旨在为无人机在实际巡检任务中的路径规划提供科学依据和技术支持。文档还展示了多个相关科研方向的案例与代码资源,涵盖路径规划、智能优化、无人机控制等多个领域。; 适合人群:具备一定Matlab编程基础,从事无人机路径规划、智能优化算法研究或自动化、控制工程方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 对比分析新型智能算法在三维复杂环境下无人机路径规划的表现差异;② 为科研项目提供可复现的算法代码与实验基准;③ 支持无人机巡检、灾害监测、电力线路巡查等实际应用场景的路径优化需求; 阅读建议:建议结合文档提供的Matlab代码进行仿真实验,重点关注不同算法在收敛速度、路径长度和避障性能方面的表现差异,同时参考文中列举的其他研究案例拓展思路,提升科研创新能力。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值