调用内联函数 C++
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果函数执行需要一些时间的操作,则情况 2 可以为您带来很大的性能。
请选择它,
Case 2 can give you a lot of performance, if the function does something that takes some time.
Choose it if
是否保留返回值或再次调用该函数的决定不应基于该函数是否内联 - 因为这是一个可能会在类的生命周期/演化过程中发生变化的实现细节。 根据经验,如果返回值不会使代码变得太复杂,我总是会保留它,因为作为类的用户,您不知道该函数的成本是多少 - 而今天便宜的可能是明天就贵了 所以我会选择案例 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.
即使性能不是问题,我也会使用案例 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.
如果函数每次调用都返回相同的结果,则必须使用情况 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
据我所知,我无法理解内联函数与将其分配给函数指针之间的关系。
但可以肯定的是,第一种方法是自我记录。
编辑 ::
谢谢艾哈迈德,
这实际上取决于,并且不能总是使用一种方法来代替另一种方法。
同样,如果两者都有效,至少第一种方法对我来说更清楚。
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.
不幸的是,没有一个简单的答案,这取决于功能。
如果函数很简单,则类似 { 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?
在不知道什么或在哪里的情况下进行优化通常会产生更复杂的代码,并且速度并不比原始代码快。
对我来说,确定使用哪种解决方案的标准并不是基于您提供的信息。
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.