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()
,哪一个会更快完成?