5.9 函数调用、继承关系性能说

#include <iostream>
#include <vector>
#include <ctime>

using namespace std;

namespace _nmsp1 //命名空间
{
	//一:函数调用中编译器的循环代码优化	
	//debug(会加入调试信息,运行时间会比较长)release(会做优化,运行时间短)
	//(1)优化循环,把循环优化成1条语句;
	//(2)在编译期间,编译器也具有运算能力,有些运算编译器在编译期间就能搞定;
	
	__int64 mytest(int mv)
	{
		__int64 icout = 0;
		for (int i = 1; i < 1000000; i++)
		{
			icout += 1;
			if (i == 10000 && mv == 999) //会影响优化,加长release下运行时长
			{
				printf("------\n");
			}
		}
		//icout += 循环多少次之后的和值
		return icout;
	}

	void func()
	{
		clock_t start, end;
		__int64 mycout = 1;
		start = clock();
		for (int i = 0; i <= 1000; i++)
		{
			//mycout += mytest(i); //给固定参数时,编译器将这种参数固定的函数调用视为一种不变的表达式
			mycout += mytest(6);
		}
		//mycout += 循环1000次的和值
		end = clock();
		cout << " 用时(毫秒):" << end - start << endl;
		cout << " mycout:" << mycout << endl;
	}
}
namespace _nmsp2
{

	//二:继承关系深度增加,开销一般也会增加
	//很多情况下,锁着继承深度的增加,开销或者说执行时间也会增加;
	//(2.1)多重继承一般也会导致开销增加

	class A
	{
	public:
		A()
		{
			cout << "A::A()" << endl;
		}
	};
	class A1
	{
	public:
		A1()
		{
			cout << "A1::A1()" << endl;
		}
	};

	class B :public A, public A1
	{
	public:
	};
	class C :public B
	{
	public:
		C()
		{
			cout << "C::C()" << endl;
		}
	};
	void func()
	{
		C cobj;

	}
}
namespace _nmsp3
{
	//三:继承关系深度增加,虚函数导致的开销增加
	class A
	{
	public:
		A()
		{
			cout << "A::A()" << endl;
		}
		virtual void myvirfunc() {}
	};

	class B :public A
	{
	public:
	};
	class C :public B
	{
	public:
		C()
		{
			cout << "C::C()" << endl;//C的构造函数里调用系统合成的B的构造函数(在B的构造函数里调用A的构造函数)
		}
	};

	void func()
	{
		C* pc = new C();
	}
}

int main()
{
	//_nmsp1::func();
	//_nmsp2::func();
	_nmsp3::func();
	return 1;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值