我可以选择性(强制)内联函数吗? [英] Can I selectively (force) inline a function?

查看:478
本文介绍了我可以选择性(强制)内联函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在书中清洁代码(以及我遇到并阅读的其他几个文章),建议保持功能小,如果它们变大,打破它们。

In the book Clean Code (and a couple of others I have come across and read) it is suggested to keep the functions small and break them up if they become large. It also suggests that functions should do one thing and one thing only.

优化软件时, C ++ Agner Fog声明,他不喜欢分割一个函数的规则,因为它跨越了多个行的某个阈值。他说这会导致不必要的跳转,从而降低性能。

In Optimizing software in C++ Agner Fog states that he does not like the rule of breaking up a function just because it crosses a certain threshold of a number of lines. He states that this results in unnecessary jumps which degrade performance.

首先,我明白,如果我工作的代码不在一个紧的循环并且函数很重,使得调用它们所花费的时间与函数中的代码执行所花费的时间相比是微不足道的。但是让我们假设我正在使用的函数,大多数时候,由其他对象/函数使用,并执行相对简单的任务。这些函数遵循第一段中列出的建议(即,执行一个单一函数,小/可理解)。然后我开始编程性能关键函数,在紧密循环中使用这些其他函数,本质上是一个帧函数。最后,假设内联它们对性能关键函数有好处,但对任何其他函数没有任何好处(是的,我已经描述了这一点,虽然有很多复制和粘贴,我想避免)。

First off, I understand that it will not matter if the code I am working on is not in a tight loop and that the functions are heavy so that the time it takes to call them is dwarfed by the time the code in the function takes to execute. But let's assume that I am working with functions that are, most of the time, used by other objects/functions and are performing relatively trivial tasks. These functions follow the suggestions listed in the first paragraph (that is, perform one single function and are small/comprehensible). Then I start programming a performance critical function that utilizes these other functions in a tight loop and is essentially a frame function. Lastly, assume that in-lining them has a benefit for the performance critical function but no benefit whatsoever to any other function (yes, I have profiled this, albeit with a lot of copying and pasting which I want to avoid).

立即,可以说标记函数 inline ,让编译器选择。但是如果我不想让所有这些函数在一个` .inl 文件中或者在标题中公开呢?在我目前的情况下,性能关键函数及其使用的其他函数都在同一个源文件中。

Immediately, one can say that tag the function inline and let the compiler choose. But what if I don't want all those functions to be in a `.inl file or exposed in the header? In my current situation, the performance critical functions and the other functions it uses are all in the same source file.

总而言之,我可以选择性地(强制)内嵌单个函数的函数,使得结束代码的行为就像是一个大函数,而不是几个调用其他函数。

To sum it up, can I selectively (force) inline a function(s) for a single function so that the end code behaves like it is one big function instead of several calls to other functions.

推荐答案

您不能强制内联。此外,现代CPU上的函数调用相对于所完成的工作的成本相当便宜。如果你的功能足够大,需要分解,那么进行调用所花费的额外时间基本上是什么。

You cannot force the inline. Also, function calls are pretty cheap on modern CPUs, compared to the cost of the work done. If your functions are large enough to need to be broken down, the additional time taken to do the call will be essentially nothing.

如果没有,你可以试试...使用宏。

Failing that, you could ... try ... to use a macro.

这篇关于我可以选择性(强制)内联函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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