#include<Windows.h>
#include <iostream>
#include<fstream>
#include<vector>
#include<list>
#include<set>
#include <map>
#include <string>


using namespace std;
#define NUM1 10 //外层循环10次
#define NUM2 100000000 //对于int、dword的1亿次i++和++i
#define NUM3 100000//对与包含10万个元素容器的iter++和++iter

#ifdef _DEBUG
#define _FNAME "debug_"
#else
#define _FNAME "release_"
#endif

typedef struct _ElemType1
{
	DWORD dw;
	_ElemType1(DWORD _dw=0):dw(_dw)
	{
	}
	bool operator<(_ElemType1 const &t1) const
	{
		return this->dw<t1.dw;
	}
}ElemType1;


typedef struct _ElemType2
{
	string str;
	_ElemType2(string _str=""):str(_str)
	{

	}
	bool operator<(_ElemType2 const &t1) const
	{
		return this->str<t1.str;

	}
}ElemType2;

void test1_int_i(ofstream &of);
void test1_dword_i(ofstream &of);
void test2_vec_type1(ofstream &of);
void test2_vec_type2(ofstream &of);
void test3_list_type1(ofstream &of);
void test3_list_type2(ofstream &of);
void test4_set_type1(ofstream &of);
void test4_set_type2(ofstream &of);
void test5_map_type1(ofstream &of);
void test5_map_type2(ofstream &of);



void test1_int_i(ofstream &of)
{
	volatile int i=0,j=0;
	DWORD dw1,dw2,total=0;
	cout<<"int类型:"<<endl;
	of<<"int类型:"<<endl;

	cout<<"i++测试:"<<endl;
	of<<"i++测试:"<<endl;

	for(;j<NUM1;++j)
	{
		dw1 = ::GetTickCount();
		while (i<NUM2)
		{
			i++;
		}
		dw2 = ::GetTickCount();
		i=0;
		cout<<j<<" : "<<dw2-dw1<<endl;
		of<<j<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i = j = 0;
	total = 0;
	cout<<"++i测试:"<<endl;
	of<<"++i测试:"<<endl;

	for(;j<NUM1;++j)
	{
		dw1 = ::GetTickCount();
		while (i<NUM2)
		{
			++i;
		}
		dw2 = ::GetTickCount();
		i=0;
		cout<<j<<" : "<<dw2-dw1<<endl;
		of<<j<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}

void test1_dword_i(ofstream &of)
{
	volatile DWORD i=0,j=0;
	DWORD dw1,dw2,total=0;
	cout<<"DWORD类型:"<<endl;
	of<<"DWORD类型:"<<endl;

	cout<<"i++测试:"<<endl;
	of<<"i++测试:"<<endl;

	for(;j<NUM1;++j)
	{
		dw1 = ::GetTickCount();
		while (i<NUM2)
		{
			i++;
		}
		dw2 = ::GetTickCount();
		i=0;
		cout<<j<<" : "<<dw2-dw1<<endl;
		of<<j<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i = j = 0;
	total = 0;
	cout<<"++i测试:"<<endl;
	of<<"++i测试:"<<endl;

	for(;j<NUM1;++j)
	{
		dw1 = ::GetTickCount();
		while (i<NUM2)
		{
			++i;
		}
		dw2 = ::GetTickCount();
		i=0;
		cout<<j<<" : "<<dw2-dw1<<endl;
		of<<j<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

}



DWORD g_dwFlag=0;
void test2_vec_type1(ofstream &of)
{
	ElemType1 t1;
	vector<ElemType1> vec1(NUM3,t1);

	DWORD dw1,dw2,total=0;
	cout<<"vector,使用type1:"<<endl;
	of<<"vector,使用type1:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	vector<ElemType1>::iterator iter;
	vector<ElemType1>::const_iterator citer;

	for(;i<NUM1;++i)
	{
		iter = vec1.begin();
		dw1 = ::GetTickCount();
		while (iter!=vec1.end())
		{

#ifdef NDEBUG
g_dwFlag+=i;
#endif


			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}

	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		iter = vec1.begin();
		while (iter!=vec1.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = vec1.begin();
		while (citer!=vec1.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = vec1.begin();
		while (citer!=vec1.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}

void test2_vec_type2(ofstream &of)
{
	ElemType2 t2;

	vector<ElemType2> vec1(NUM3,t2);

	DWORD dw1,dw2,total=0;
	cout<<"vector,使用type2:"<<endl;
	of<<"vector,使用type2:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	vector<ElemType2>::iterator iter;
	vector<ElemType2>::const_iterator citer;


	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		iter = vec1.begin();
		while (iter!=vec1.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		iter = vec1.begin();
		while (iter!=vec1.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = vec1.begin();
		while (citer!=vec1.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = vec1.begin();
		while (citer!=vec1.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}

void test3_list_type1(ofstream &of)
{
	ElemType1 t1;
	list<ElemType1> ls(NUM3,t1);

	DWORD dw1,dw2,total=0;
	cout<<"list,使用type1:"<<endl;
	of<<"list,使用type1:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	list<ElemType1>::iterator iter;
	list<ElemType1>::const_iterator citer;


	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		iter = ls.begin();
		while (iter!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		iter = ls.begin();
		while (iter!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = ls.begin();
		while (citer!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = ls.begin();
		while (citer!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}

void test3_list_type2(ofstream &of)
{
	ElemType2 t2;
	list<ElemType2> ls(NUM3,t2);

	DWORD dw1,dw2,total=0;
	cout<<"list,使用type2:"<<endl;
	of<<"list,使用type2:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	list<ElemType2>::iterator iter;
	list<ElemType2>::const_iterator citer;


	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		iter = ls.begin();
		while (iter!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		iter = ls.begin();
		while (iter!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = ls.begin();
		while (citer!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		dw1 = ::GetTickCount();
		citer = ls.begin();
		while (citer!=ls.end())
		{
#ifdef NDEBUG
			g_dwFlag+=i;
#endif
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}


void test4_set_type1(ofstream &of)
{
	set<ElemType1> ls;
	for (int n=0;n<NUM3;++n)
	{
		ls.insert(_ElemType1(n));
	}

	DWORD dw1,dw2,total=0;
	cout<<"set,使用type1:"<<endl;
	of<<"set,使用type1:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	set<ElemType1>::iterator iter;
	set<ElemType1>::const_iterator citer;


	for(;i<NUM1;++i)
	{
		iter = ls.begin();

		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		iter = ls.begin();
		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}
void test4_set_type2(ofstream &of)
{

	set<ElemType2> ls;
	char sz[255];
	for (int n=0;n<NUM3;++n)
	{
		itoa(n,sz,10);
		ls.insert(ElemType2(sz));
	}

	DWORD dw1,dw2,total=0;
	cout<<"set,使用type2:"<<endl;
	of<<"set,使用type2:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	set<ElemType2>::iterator iter;
	set<ElemType2>::const_iterator citer;


	for(;i<NUM1;++i)
	{
		iter = ls.begin();
		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		iter = ls.begin();

		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}
void test5_map_type1(ofstream &of)
{
	map<int,ElemType1> ls;

	for (int n=0;n<NUM3;++n)
	{
		ls.insert(pair<int,ElemType1>(n,ElemType1(n)));
	}

	DWORD dw1,dw2,total=0;
	cout<<"map,使用type1:"<<endl;
	of<<"map,使用type1:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	map<int,ElemType1>::iterator iter;
	map<int,ElemType1>::const_iterator citer;


	for(;i<NUM1;++i)
	{
		iter = ls.begin();
		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		iter = ls.begin();

		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

}
void test5_map_type2(ofstream &of)
{
	map<int,ElemType2> ls;

	char sz[255];
	for (int n=0;n<NUM3;++n)
	{
		itoa(n,sz,10);
		ls.insert(pair<int,ElemType2>(n,ElemType2(sz)));
	}

	DWORD dw1,dw2,total=0;
	cout<<"map,使用type2:"<<endl;
	of<<"map,使用type2:"<<endl;

	cout<<"iter++测试:"<<endl;
	of<<"iter++测试:"<<endl;

	int i = 0,j=0;
	map<int,ElemType2>::iterator iter;
	map<int,ElemType2>::const_iterator citer;


	for(;i<NUM1;++i)
	{
		iter = ls.begin();
		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			iter++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;

	cout<<"++iter测试:"<<endl;
	of<<"++iter测试:"<<endl;

	for(;i<NUM1;++i)
	{
		iter = ls.begin();

		dw1 = ::GetTickCount();
		while (iter!=ls.end())
		{
			++iter;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;


	i=0;
	total=0;
	cout<<"citer++测试:"<<endl;
	of<<"citer++测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			citer++;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;

	i=0;
	total=0;
	cout<<"++citer测试:"<<endl;
	of<<"++citer测试:"<<endl;

	for(;i<NUM1;++i)
	{
		citer = ls.begin();

		dw1 = ::GetTickCount();
		while (citer!=ls.end())
		{
			++citer;
		}
		dw2 = ::GetTickCount();

		cout<<i<<" : "<<dw2-dw1<<endl;
		of<<i<<" : "<<dw2-dw1<<endl;
		total+=dw2-dw1;
	}
	cout<<"总消耗:"<<total<<endl;
	of<<"总消耗:"<<total<<endl;
	cout<<"平均消耗:"<<float(total)/NUM1<<endl;
	of<<"平均消耗:"<<float(total)/NUM1<<endl;

	cout<<endl;
	of<<endl;
}

#define LOGFILE_PATH_A L"qin_nkl_23529303.etl"
int main()
{
	string strFileName = _FNAME;
	string strTime = __TIME__;
	for(int k=0;k<strTime.length();++k)
		if(strTime[k]==':')
			strTime[k]='-';
	strFileName+=strTime;
	strFileName+="_test1.txt";

	//文件测试用
	char sz[MAX_PATH];
	sprintf(sz,"F:\\parse_%s_%d.txt",LOGFILE_PATH_A,GetTickCount());
	cout<<sz<<endl;

	sprintf(sz,"F:\\parse_%S_%d.txt",LOGFILE_PATH_A,GetTickCount());
	cout<<sz<<endl;

	cin.get();
	return 0;

	//strFileName = sz;
	ofstream of(strFileName.c_str());

	of<<GetTickCount()<<endl;
	of.flush();
	of.close();
	cin.get();
	return 0;

	test1_int_i(of);
	test1_dword_i(of);
	test2_vec_type1(of);
	test2_vec_type2(of);
	test3_list_type1(of);
	test3_list_type2(of);
	test4_set_type1(of);
	test4_set_type2(of);
	test5_map_type1(of);
	test5_map_type2(of);


	cout<<"标记:"<<g_dwFlag<<endl;
	of.flush();
	of.close();
	cin.get();
	return 0;
}



vector/list/set/map 遍历耗时统计的更多相关文章

  1. map遍历的四种方式

    原文 http://blog.csdn.net/dayanxuqun/article/details/26348277 以下是map遍历的四种方式: // 一.推荐只用value的时候用,都懂的... ...

  2. java map遍历方式及效率

    本文转载自Java Map遍历方式的选择. 只给出遍历方式及结论.测试数据可以去原文看. 如果你使用HashMap 同时遍历key和value时,keySet与entrySet方法的性能差异取决于ke ...

  3. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  4. springMVC Aspect AOP 接口耗时统计

    在接口开发中,我们通常需要统计接口耗时,为后续接口性能做统计.在springMVC中可以用它的aop来记录日志. 1.在spring配置文件中开启AOP <!--*************** ...

  5. js中三个对数组操作的函数 indexOf()方法 filter筛选 forEach遍历 map遍历

     indexOf()方法  indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. 不使用indexOf时 var arr = ['apple','orange','pea ...

  6. 原生JS forEach()和map()遍历的区别以及兼容写法

    一.原生JS forEach()和map()遍历 共同点: 1.都是循环遍历数组中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前 ...

  7. map遍历性能记录

    map遍历可以通过keySet或者entrySet方式. 性能上:entrySet略胜一筹,原因是keySet获取到key后再根据key去获取value,在查一遍,所以慢一些. keySet: //先 ...

  8. forEach() 和 map() 遍历

    1.forEach()   没有返回值. arr[].forEach(function(value,index,array){ //do something }) 参数:value数组中的当前项, i ...

  9. js的map遍历和array遍历

    1. array遍历: [1].forEach() forEach是ES5中操作数组的一种方法,主要功能是遍历数组.forEach方法中的function回调有三个参数:第一个参数是遍历的数组内容,第 ...

随机推荐

  1. windows_study_1

    描述:win8/windows server 设置用户登陆密码永不过期 解决:  第一步:打开控制面板,点击系统和安全第二部:管理工具第三步:本地安全组策略第四步:看图 第五步:把密码过期天数,改成0 ...

  2. 洛谷 P3233 [HNOI2014]世界树(虚树+dp)

    题面 luogu 题解 数据范围已经告诉我们是虚树了,考虑如何在虚树上面\(dp\) 以下摘自hzwer博客: 构建虚树以后两遍dp处理出虚树上每个点最近的议事处 然后枚举虚树上每一条边,考虑其对两端 ...

  3. QQ链接病毒分析

    QQ链接病毒分析 特征 点击病毒链接后,自动会在每一时刻范围内通过所有途径群发新的病毒链接(途径包括Qzone,群聊等) 分析 首先看一下病毒链接的一个样例 http://news.soso.com/ ...

  4. 04-树6 Complete Binary Search Tree (30 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  5. python爬虫之User-Agent用户信息

    python爬虫之User-Agent用户信息 爬虫是自动的爬取网站信息,实质上我们也只是一段代码,并不是真正的浏览器用户,加上User-Agent(用户代理,简称UA)信息,只是让我们伪装成一个浏览 ...

  6. 使用kafka bin目录中的zookeeper-shell.sh来查看kafka在zookeeper中的配置

    cd kafka_2.11-0.10.2.1\bin\windowsecho ls /brokers/ids |  zookeeper-shell.bat localhost:2181 使用kafka ...

  7. 转 .net 获取IP地址的三个方法的比较

    获取用户IP地址的三个属性的区别(HTTP_X_FORWARDED_FOR,HTTP_VIA,REMOTE_ADDR)   一.没有使用代理服务器的情况: REMOTE_ADDR = 您的 IP    ...

  8. windows下dubbo-admin的安装

    本来以为十分钟就能搞定的东西结果搞了一个小时,也是菜到抠脚,赶紧记录一下. 下载dubbo源码,下载地址:https://download.csdn.net/download/huangzhang_/ ...

  9. Java基础26-对象初始化过程

    /* 1.因为new Test1()用到了Test1类,所以会把它从硬盘上加载进入内存 2.如果有static静态代码块就会随着类的加载而执行,还有静态成员和普通方法也会随着类的加载而被加载 3.在堆 ...

  10. IE Error: '__doPostBack' is undefined 问题解决

    突然遇到个很奇怪的BUG,翻页控件,其他浏览器一切正常,IE无法翻页,会提示 '__doPostBack' is undefined 后来搜索发现: [原文發表地址] Bug and Fix: ASP ...