调用内联函数 C++

发布于 2024-07-25 19:05:48 字数 300 浏览 4 评论 0原文

我在 MyClass 类下定义了一个内联成员函数,

int MyClass::myInlineFunction();

该函数是从我的代码中的多个位置调用的。
有两种方法可以调用该函数
情况 1:每次调用该函数时都使用它。

 mobj->myInlineFunction() ;

情况2:将这个函数的结果赋值给一个变量并用于后续访问

 var = mobj->myInlineFunction() ;

应该首选哪一个?

I have an inline member function defined under class MyClass

int MyClass::myInlineFunction();

This function is called from several places in my code.
There are two ways to call this function
Case 1: Using this every time the function is called.

 mobj->myInlineFunction() ;

Case 2: Assign the result of this function to a variable and use it for subsequent accesses

 var = mobj->myInlineFunction() ;

Which one should be preferred??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浸婚纱 2024-08-01 19:05:48

如果函数执行需要一些时间的操作,则情况 2 可以为您带来很大的性能。
请选择它,

  • 如果您不需要函数发生副作用,
  • 该函数将始终在该上下文中返回相同的结果

Case 2 can give you a lot of performance, if the function does something that takes some time.
Choose it if

  • you do not need side effects of the function to happen
  • the function would always the return the same result in that context
甜味拾荒者 2024-08-01 19:05:48

是否保留返回值或再次调用该函数的决定不应基于该函数是否内联 - 因为这是一个可能会在类的生命周期/演化过程中发生变化的实现细节。 根据经验,如果返回值不会使代码变得太复杂,我总是会保留它,因为作为类的用户,您不知道该函数的成本是多少 - 而今天便宜的可能是明天就贵了 所以我会选择案例 2。

The decision on whether to hold onto a return value or to recall the function again, should not be based on whether the function is inlined or not - as this is an implementation detail that may change over the lifetime/evolution of the class. As a rule of thumb I would always hang onto the return value if it does not make the code too complicated, as you do not know as a user of a class, what the cost of the function is - and what is cheap today could be expensive tomorrow. So I would go with Case 2.

傲影 2024-08-01 19:05:48

即使性能不是问题,我也会使用案例 2。 如果您只关心该函数的结果,那么将其分配给变量可以让您稍后轻松切换到另一种方法来获取该结果。

I would use Case 2 even if performance is not a problem. If all you care is the result of this function, then assigning it to a variable allows you later to easily switch to another method of obtaining this result.

方圜几里 2024-08-01 19:05:48

如果函数每次调用都返回相同的结果,则必须使用情况 2,因为使用情况 1 没有意义,即使它是内联函数

If the function returns the same result for every call, you have to use case 2 as it does not make sense to use case 1 even if it is inline function

内心旳酸楚 2024-08-01 19:05:48

据我所知,我无法理解内联函数与将其分配给函数指针之间的关系。

但可以肯定的是,第一种方法是自我记录。

编辑 ::
谢谢艾哈迈德,

这实际上取决于,并且不能总是使用一种方法来代替另一种方法。

同样,如果两者都有效,至少第一种方法对我来说更清楚。

I can not understand the relation between inlining the function and assigning it to a function pointer as I understand.

But for sure, the first way is self-documenting.

EDIT ::
Thanks Ahmed,

It depends actually and one can not always be used instead of the other way.

Again, the first way is clearer for me at least, if both work.

伴我心暖 2024-08-01 19:05:48

不幸的是,没有一个简单的答案,这取决于功能。

如果函数很简单,则类似 { return 6; },每次调用它即可。 编译器将内联调用并删除代码。 您应该发现将其分配给变量与不将其分配给变量之间没有区别。

如果函数更复杂,那么可能值得将其分配给变量。 该值在整个程序中是否恒定? 那么也许可以向类添加一个包含该值的静态成员?

Unfortunatly there isn't one simple answer, it depends on the function.

If the function is trivial, something like { return 6; }, just call it each time. The compiler will inline the call and remove the code. You should find no difference between assigning it to a variable, or not.

If the function is more complex, then it might be worth assigning it to a variable. Is the value constant throughout the program? Then maybe add to the class a static member which contains the value?

混浊又暗下来 2024-08-01 19:05:48

在不知道什么或在哪里的情况下进行优化通常会产生更复杂的代码,并且速度并不比原始代码快。

对我来说,确定使用哪种解决方案的标准并不是基于您提供的信息。

  • 如果该方法是访问器(仅返回内部值,如 std::vector<>::size() ),则使其内联并且不缓存结果。
  • 如果该方法确实执行了重要的工作,请不要将其内联并测量(您无法测量内联方法)它被调用的次数以及它代表的成本。 然后决定缓存或内联是否合适。
  • 缓存结果(你的情况2)可以改进昂贵的方法,但前提是保证该值在使用之间不会改变,在这种情况下,我不会内联该方法。
  • 如果对算法产生负面影响,则必须缓存结果(根据结果创建内存,然后使用内存检查溢出相同的结果,如果值发生变化,您将溢出缓冲区)

Optimizing without knowing what or where usually ends up with more complex code that is not faster than the original.

To me the criteria to determine what solution to use is not based in the information that you provide.

  • If the method is an accessor (just returns an internal value, as in std::vector<>::size() ) then make it inline and do not cache the result.
  • If the method does perform significant work, don't make it inline and measure (you cannot measure an inlined method) how many times it is called, what cost it represents. Then decide if caching or inlining is apropriate.
  • Caching the result (your case 2) can be an improvement for expensive methods, but only if the value is guaranteed not to change between the uses, and in this case I would not make the method inlined.
  • You must cache the result if if affects your algorithm negatively (create memory based on the result and then use that memory checking overruns with that same result, if the value changes you will overrun your buffer)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文