问题
class MyClass
{
public:
MyClass()
{
m_dbLoopStart = 0.0;
m_dbLoopStop = 100.0;
m_dbLoopStep = 0.001;
}
// Which of the following methods complete in a shorter time?
void Foo1() const // This one?
{
for (double x=m_dbLoopStart; x<=m_dbLoopStop; x+=m_dbLoopStep)
{
f(x);
}
}
void Foo2() const // Or, this one?
{
for (double x=m_dbLoopStart; x<=m_dbLoopStop; x+=m_dbLoopStep)
{
2.0 * x + 1.0;
}
}
private:
double m_dbLoopStart, m_dbLoopStop, m_dbLoopStep;
inline static double f(double x)
{
return 2.0 * x + 1.0;
}
};
Between Foo1()
and Foo2()
, which one would complete faster?
回答1:
Inline functions may be faster because they avoid the cost of a function call and return overhead. Remember that compilers may choose not to inline functions.
In most translators, function parameters must be passed and space allocated for the parameters and any function local storage. On the return side, the function local variables must be removed and usually a value returned.
For simple functions, such as getters and setters, the overhead of the function call and return are greater than the code in the function. Thus inlining will speed up these functions.
Inline functions also remove the branch instruction to call the function. This reduces the chance of the processor clearing out the instruction cache / pipeline. Although modern processors have implemented algorithms to reduce the negative impact of branch instructions.
In my programming practices, I inline small (3 line or smaller) methods. If I'm going to inline for performance reasons, I'll profile before inlining.
回答2:
In this case, the use of "inline" is superfluous because functions defined in the class definition are inline by default.
Having said this, defining a function as inline doesn't mean that the function must be inlined and not defining it as inline doesn't mean that the compiler will not inline the function.
As others already said, in this case, it won't make a difference, with optimization on, even the loop should be optimized to being no code, with x being assigned the value of m_dbLoopStop (whatever x may be).
回答3:
if you use an inline function, the compiler may still opt not to copy the body of the function itself, thus causing a function call. If you write the function body explicitly then for sure no functions are called.
So strictly theoretically, its faster.
回答4:
There are often slight semantic differences between calling inline functions with parameters vs simply using copy/paste code or #define macros. For example, consider the macro and function:
extern void some_extern_function(int x); #define foo1(x) (some_extern_function((x)), some_extern_function((x))) void inline foo2(int x) {some_extern_function(x); some_extern_function(x); }
Now suppose one invokes them:
extern volatile int some_volatile_int; foo1(some_volatile_int); foo2(some_volatile_int);
In this scenario, the inline function foo2
must make a copy of some_volatile_int
and then pass that copy to both calls of some_extern_function
. By contrast, the macro must load some_volatile_int
twice. Depending upon the calling conventions, either approach may be faster than the other.
来源:https://stackoverflow.com/questions/8712967/is-using-an-inline-function-as-fast-as-directly-writing-the-function-body-in-the