码迷,mamicode.com
首页 > 编程语言 > 详细

读《深度探索C++对象模型》之对象成员的效率

时间:2016-05-13 02:26:07      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

测试平台:华硕N53S(五年前的老机子)

编译环境:VS2010

接下来我将会有多个测试,在多个不同环境下的所消耗的时间比较:

五个测试分别为:个别的局部变量、局部数组、struct之的Public、class 之中的inline Get函数、 class之中的inline Get & Set函数:

代码片段如下:

struct Point
{
	Point(float mx, float my, float mz)
	{
		x = mx; y = my; z = mz;
	}
	float x, y, z;
};

class Point3D
{
public:
	Point3D(float xx = 0.0f, float yy = 0.0f, float zz = 0.0f)
		:_x(xx), _y(yy), _z(zz)
	{

	}

	float &x () {return _x;}
	float &y () {return _y;}
	float &z () {return _z;}

	void x(float nx) {_x = nx;}
	void y(float ny) {_y = ny;}
	void z(float nz) {_z = nz;}

private:
	float _x, _y, _z;
};

int main()
{
	double dur;
	clock_t start, end;
	//local 
	float pA_x = 1.725f, pA_y = 0.875f, pA_z = 0.478f;
	float pB_x = 0.315f, PB_y = 0.317f, PB_z = 0.838f;
	start = clock();
	for (unsigned i = 0; i < 100000000; ++i)
	{
		pB_x = pA_x - PB_z;
		PB_y = pA_y - pB_x;
		PB_z = pA_z - PB_y;
	}
	end = clock();
	dur = double(end - start);
	printf("Use time 1 : %f\n", (dur/CLOCKS_PER_SEC));
	//
	enum fussy{x, y, z};
	float pA[3] = {1.725f, 0.875f, 0.478f};
	float pB[3] = {0.315f, 0.317f, 0.838f};
	start = clock();
	for (unsigned i = 0; i < 100000000; ++i)
	{
		pB[x] = pA[x] - pB[z];
		pB[y] = pA[y] + pB[x];
		pB[z] = pA[z] + pB[y];
	}
	end = clock();
	dur = double(end - start);
	printf("Use time 2 : %f\n", (dur/CLOCKS_PER_SEC));

	Point sPa(1.725f, 0.875f, 0.478f);
	Point sPb(0.315f, 0.317f, 0.838f);
	start = clock();
	for (unsigned i = 0; i < 100000000; ++i)
	{
		sPb.x = sPa.x - sPb.z;
		sPb.y = sPa.y + sPb.x;
		sPb.z = sPa.z + sPb.y;
	}
	end = clock();
	dur = double(end - start);
	printf("Use time 3 : %f\n", (dur/CLOCKS_PER_SEC));

	Point3D cpA(1.725f, 0.875f, 0.478f);
	Point3D cpB(0.315f, 0.317f, 0.838f);
	start = clock();
	for (unsigned i = 0; i < 100000000; ++i)
	{
		cpB.x() = cpA.x() - cpB.z();
		cpB.y() = cpA.y() + cpB.x();
		cpB.z() = cpA.z() + cpB.y();
	}
	end = clock();
	dur = double(end - start);
	printf("Use time 4 : %f\n", (dur/CLOCKS_PER_SEC));

	Point3D cpA1(1.725f, 0.875f, 0.478f);
	Point3D cpB1(0.315f, 0.317f, 0.838f);
	start = clock();
	for (unsigned i = 0; i < 100000000; ++i)
	{
		cpB1.x(cpA1.x() - cpB1.z());
		cpB1.y(cpA1.y() + cpB1.x());
		cpB1.z(cpA.z() + cpB.y());
	}
	end = clock();
	dur = double(end - start);
	printf("Use time 5 : %f\n", (dur/CLOCKS_PER_SEC));


	system("pause");
	return 0;
}

在不打开如何优化的时间下,所用的时间如下:

技术分享

可以看出,在不优化的情况下,前面三种几乎时间是一样的,因为可以说是C的用法,后面两种是C++的用法,所消耗的时间几乎是前两种的2倍,应该是由于封装所带来的时间吧。下面在打卡vs2010的全部优化开关的情况下所用的时间:

技术分享

可以看到,在优化全部打开的情况下,五种情况下的时间,基本上都为1.165ms,类所带来的封装也就没有带来什么执行期的效率成本了。

那如果加上继承呢,那我们再次来验证一下:

单一的继承:

优化的情况下:

技术分享

未优化的情况下:

技术分享

虚拟继承(单一)(指的是Point2d虚拟继承于Point1d):

优化的情况下:

技术分享

未优化的情况下:

技术分享

在多层的虚拟继承情况下呢:

优化的情况下:

技术分享

未优化的情况下:

技术分享

可以看到,对于虚拟继承来说,所消耗的时间会比较多些,可能是因为我的测试比较简单,不是很明显。

所以能不用虚拟继承,就不要用吧,除非没办法了。

读《深度探索C++对象模型》之对象成员的效率

标签:

原文地址:http://blog.csdn.net/smsmn/article/details/51345451

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!