据我所知,继承层次结构中的C ++类中的虚函数调用应该比不从任何基类继承的等效类(即自包含类)慢一点。我决定写一个小测试程序来看看性能的差异。
我有一个由3个类组成的继承层次结构:Shape,Rectangle,Quadrilateral。我有一个名为BaseQuadrilateral的类,它不会从任何东西继承,并且做与Quadrilateral类相同的事情。每个类有两种方法:surfaceArea()和volume()。我在每个类上运行一个单独的基准测试,并记录运行10,000,000个对象所需的时间。我预计四边形课程会花费更长的时间。相反,Quadrilateral类(继承自Rectangle)的运行速度比BaseQuadrilateral快。我不明白为什么会这样。
Test Results:
Running Dynamic Dispatch Test
Quadrilateral Runtime: 2840264 Ticks, 2 Seconds.
BaseQuadrilateral Runtime: 21179219 Ticks, 21 Seconds.
有人可以向我解释幕后发生的事情会使继承的代码更快,在什么情况下,继承的代码运行速度会慢于非继承的代码。
谢谢
class Shape
{
public:
virtual double surfaceArea() = 0;
virtual double Volume() = 0;
};
class Rectangle : public Shape
{
public:
//Constructors
Rectangle();
Rectangle(double, double);
Rectangle(const Rectangle&);
Rectangle& operator=(const Rectangle&);
double Area();
//Override Shape base class methods
double surfaceArea();
double Volume();
protected:
double length;
double width;
};
class Quadrilateral : public Rectangle
{
public:
//Constructors
Quadrilateral();
Quadrilateral(double, double, double);
Quadrilateral(const Quadrilateral&);
Quadrilateral& operator=(const Quadrilateral&);
//Overloaded Square base class
double surfaceArea();
double Volume();
protected:
double height;
};
class BaseQuadrilateral
{
public:
//Constructors
BaseQuadrilateral();
BaseQuadrilateral(double, double, double);
BaseQuadrilateral(const BaseQuadrilateral&);
BaseQuadrilateral& operator=(const BaseQuadrilateral&);
double surfaceArea();
double Volume();
protected:
double length;
double width;
double height;
};
void test2()
{
clock_t qTimer, bqTimer;
Quadrilateral* quadrilaterals;
BaseQuadrilateral* baseQuadrilaterals, baseQuadrilateral;
Shape* shape;
double* answers1, *answers2;
srand((unsigned int)time(NULL));
cout << "Running Dynamic Dispatch Test\n" << endl;
quadrilaterals = new Quadrilateral[ARRAY_SIZE];
baseQuadrilaterals = new BaseQuadrilateral[ARRAY_SIZE];
answers1 = new double[ARRAY_SIZE];
answers2 = new double[ARRAY_SIZE];
//Initialization
for (int i = 0; i < ARRAY_SIZE; i++)
{
double length = (double)(rand() % 100);
double width = (double)(rand() % 100);
double height = (double)(rand() % 100);
quadrilaterals[i] = Quadrilateral(length, width, height);
baseQuadrilaterals[i] = BaseQuadrilateral(length, width, height);
}
//Test Shape
qTimer = clock();
for (int i = 0; i < ARRAY_SIZE; i++)
{
shape = &quadrilaterals[i];
answers1[i] = shape->Volume();
}
qTimer = clock() - qTimer;
//Test BaseQuadrilateral
bqTimer = clock();
for (int i = 0; i < ARRAY_SIZE; i++)
{
baseQuadrilateral = baseQuadrilaterals[i];
answers2[i] = baseQuadrilateral.Volume();
}
bqTimer = clock() - qTimer;
for (int i = 0; i < ARRAY_SIZE; i++)
{
if (answers1[i] != answers2[i])
{
cout << "Incorrect answer found at i=" << i << ". answers1: " << answers1[i] << " answers2: " << answers2[i] << endl;
break;
}
}
//Print Results
cout << "Quadrilateral Runtime: " << qTimer << " Ticks, " << qTimer / CLOCKS_PER_SEC << " Seconds." << endl;
cout << "BaseQuadrilateral Runtime: " << bqTimer << " Ticks, " << bqTimer / CLOCKS_PER_SEC << " Seconds." << endl;
}
答案 0 :(得分:3)
正如我在评论中写的那样,你对这一行有一点问题:
bqTimer = clock() - qTimer;
。您似乎希望qTimer
为bqTimer
。然而,这并不能解释您观察到的差异。为此,您应该仔细查看两个测试循环,特别是它们的不同之处。
在第一种情况下,您正在变量Quadrilateral
中记录指向shape
的指针,然后通过指针间接调用Volume()
方法:
for (int i = 0; i < ARRAY_SIZE; i++)
{
shape = &quadrilaterals[i];
answers1[i] = shape->Volume();
}
在第二种情况下,您正在复制整个BaseQuadrilateral
对象,然后调用副本的Volume()
方法:
for (int i = 0; i < ARRAY_SIZE; i++)
{
baseQuadrilateral = baseQuadrilaterals[i];
answers2[i] = baseQuadrilateral.Volume();
}
复制对象比获取对象要昂贵得多。实际上,在这种特殊情况下,甚至可以完全优化地址计算。我建议避免使用中间件,并且在两种情况下直接在数组元素上调用方法:
answers1[i] = quadrilaterals[i].Volume();
或
answers2[i] = baseQuadrilaterals[i].Volume();
答案 1 :(得分:0)
该行
bqTimer = clock() - qTimer;
是
bqTimer = clock() - bqTimer;