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;
}
};
在Foo1()
和Foo2()
之间,哪一个会更快完成?使用内联函数与直接在代码中写入函数体一样快吗?
如果完全不同,那么你需要一个更好的编译器。 – 2012-01-03 13:28:51
这些都应该导致非常相似的程序集(如果不是相同的话)。 – 2012-01-03 13:28:58
唯一的区别是内联函数可能不一定*被内联,而直接插入代码则编译器没有选择。 – 2012-01-03 13:31:03