⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_map.cpp

📁 TFixedAlloc类是一个非常不错的使用与Linux和windows跨平台的内存分配工具
💻 CPP
字号:
#include <iostream>
#include <stdio.h>
using namespace std;

#ifdef _WIN32
#include <conio.h>
#else //linux
#include <ctype.h>
#include <dlfcn.h>
//about time header file
#include <sys/time.h>
#include <unistd.h>
	
#endif
	

#ifdef _WIN32
HMODULE		g_hModule = 0;
#define MyGetChar()		getche()
#else //linux
void *		g_hModule = 0;
#define MyGetChar()		getchar()
#endif
	
#include <string>
#include <map>
using std::string;
using std::map;

#include "TMap.h"

//字符串访问测试
typedef map< string,void * > TMAPDATAAREA;
TMAPDATAAREA	g_mapDataArea;

void InitDataArea();
void Test_USec(string& strFind);
//整数访问测试
typedef map< unsigned long,void * > TMAPDATAAREA2;
TMAPDATAAREA2	g_mapDataArea2;

void InitDataArea2();
void Test_USec(unsigned long ulFind);

//TMapStringToPtr进行测试
typedef TMapStringToPtr TMAPDATAAREA3;
TMAPDATAAREA3	g_mapDataArea3;

void InitDataArea3();
void Test_MapStringToPtr(string& strFind);


int main(int argc, char* argv[])
{
	cout << "Start up my Test_Map program success!" << endl;
	cout << "******************************************************************" << endl;
	cout << "********************************Help******************************" << endl;
	cout << "********Press \"Q\" or \"q\" exit system**************************" << endl;
	cout << "\t 1 - Func1() --Find str-first\t " << endl;
	cout << "\t 2 - Func2() --Find str-Middle\t " << endl;
	cout << "\t 3 - Func3() --Find str-end\t " << endl;
	cout << "\t 4 - Func4() --Find ul-first\t " << endl;
	cout << "\t 5 - Func5() --Find ul-Middle\t " << endl;
	cout << "\t 6 - Func6() --Find ul-end\t " << endl;	
	cout << "\t 7 - Func7() --Find str-first by TMapStringToPtr\t " << endl;
	cout << "\t 8 - Func8() --Find str-Middle by TMapStringToPtr\t " << endl;
	cout << "\t 9 - Func9() --Find str-end by TMapStringToPtr\t " << endl;	

	while( 1 )
	{
		int key;
		key = MyGetChar();
		key = toupper( key );
		cout << endl;
		if( key == 'Q' )
		{
			cout << "Exit from TestTCPComm_Client program" << endl;
			break;
		}
		else
		{
			string strFind;
			unsigned long ulFind;
			switch( key )
			{
				case '1':  //Func1() --first	
					strFind = "First";
					Test_USec(strFind);
					break;
				case '2':	//Func2() --Mid
					strFind = "GetDName";
					Test_USec(strFind);
					break;
				case '3':	//Func3() --end
					strFind = "当然高";
					Test_USec(strFind);
					break;	
				case '4':  //Func4() --first	
					ulFind = 1000;
					Test_USec(ulFind);
					break;
				case '5':	//Func5() --Mid
					ulFind = 888888888;
					Test_USec(ulFind);
					break;
				case '6':	//Func6() --end
					ulFind = 10;//2147483648;
					Test_USec(ulFind);
					break;
				case '7':  //Func7() --first by TMapStringToPtr	
					strFind = "First";
					Test_MapStringToPtr(strFind);
					break;
				case '8':	//Func8() --Mid by TMapStringToPtr 
					strFind = "GetDName";
					Test_MapStringToPtr(strFind);
					break;
				case '9':	//Func9() --end by TMapStringToPtr
					strFind = "当然高";
					Test_MapStringToPtr(strFind);
					break;						
				default:
					;//cout << "error command" << endl;
			}
		} //end if
	}

	return 0;
}


//字符串访问测试
void Test_USec(string& strFind)
{
	InitDataArea();
	struct timeval TimeValue1;
	if( gettimeofday(&TimeValue1,NULL)==-1 )
	{
		printf( "Get time failed,88\n" );
		return;
	}
	int i=0;
	for( unsigned long i=0; i<10000000; ++i )
	{
		TMAPDATAAREA::iterator  iter = g_mapDataArea.find(strFind);
		if( iter == g_mapDataArea.end() )  
			printf( "not find - %s \n",strFind.c_str() );
		else
		{	
			i++;
			if( i==1 )
				printf( "find - %s \n",strFind.c_str() );
		}
	}
	struct timeval TimeValue2;
	if( gettimeofday(&TimeValue2,NULL)==-1 )
	{
		printf( "Get time failed,88\n" );
		return;
	}
	double timeuse;
	timeuse = 1000000*(TimeValue2.tv_sec-TimeValue1.tv_sec) +
				TimeValue2.tv_usec-TimeValue1.tv_usec;
	printf( "Time elapsed second:%f\n", timeuse/1000000);
	printf( "Time elapsed micro-second:%f\n", timeuse);
}

void InitDataArea()
{
	g_mapDataArea.clear();
	g_mapDataArea["First"] = (void *)NULL;  	//1
	g_mapDataArea["ertetre"] = (void *)NULL;	//2
	g_mapDataArea["6ytrtuu"] = (void *)NULL;	//3
	g_mapDataArea["5756h76"] = (void *)NULL;	//4
	g_mapDataArea["457tg"] = (void *)NULL;	//5
	g_mapDataArea["GetDName"] = (void *)NULL;	//6
	g_mapDataArea["erttytt"] = (void *)NULL;	//7
	g_mapDataArea["adfgf"] = (void *)NULL;	//8
	g_mapDataArea["ht765"] = (void *)NULL;	//9
	g_mapDataArea["urt敢"] = (void *)NULL;	//10
	g_mapDataArea["当然高"] = (void *)NULL;	//11
}

//整数访问测试
void Test_USec(unsigned long ulFind)
{
	InitDataArea2();
	struct timeval TimeValue1;
	if( gettimeofday(&TimeValue1,NULL)==-1 )
	{
		printf( "Get time failed,88\n" );
		return;
	}
	int i=0;
	for( unsigned long i=0; i<10000000; ++i )
	{
		TMAPDATAAREA2::iterator  iter = g_mapDataArea2.find(ulFind);
		if( iter == g_mapDataArea2.end() )  
			printf( "not find - %ld \n",ulFind );
		else
		{	
			i++;
			if( i==1 )
				printf( "find - %ld \n",ulFind );
		}
	}
	struct timeval TimeValue2;
	if( gettimeofday(&TimeValue2,NULL)==-1 )
	{
		printf( "Get time failed,88\n" );
		return;
	}
	double timeuse;
	timeuse = 1000000*(TimeValue2.tv_sec-TimeValue1.tv_sec) +
				TimeValue2.tv_usec-TimeValue1.tv_usec;
	printf( "Time elapsed second:%f\n", timeuse/1000000);
	printf( "Time elapsed micro-second:%f\n", timeuse);
}

void InitDataArea2()
{
	g_mapDataArea2.clear();
	g_mapDataArea2[1000] = (void *)NULL;  	//1
	g_mapDataArea2[234234] = (void *)NULL;	//2
	g_mapDataArea2[34534554] = (void *)NULL;	//3
	g_mapDataArea2[23] = (void *)NULL;	//4
	g_mapDataArea2[34344] = (void *)NULL;	//5
	g_mapDataArea2[888888888] = (void *)NULL;	//6
	g_mapDataArea2[56545670] = (void *)NULL;	//7
	g_mapDataArea2[95675] = (void *)NULL;	//8
	g_mapDataArea2[56456655] = (void *)NULL;	//9
	g_mapDataArea2[44556556] = (void *)NULL;	//10
	//g_mapDataArea2[2147483648] = (void *)NULL;	//11
	g_mapDataArea2[10] = (void *)NULL;	//11
}

//TMapStringToPtr进行测试
void Test_MapStringToPtr(string& strFind)
{
	InitDataArea3();
	struct timeval TimeValue1;
	if( gettimeofday(&TimeValue1,NULL)==-1 )
	{
		printf( "Get time failed,88\n" );
		return;
	}
	int i=0;
	for( unsigned long i=0; i<10000000; ++i )
	{
		void * pTest;
		if( g_mapDataArea3.Lookup(strFind.c_str(),pTest)==TFALSE )
			printf( "not find - %s \n",strFind.c_str() );
		else
		{	
			i++;
			if( i==1 )
				printf( "find - %s \n",strFind.c_str() );
		}
	}
	struct timeval TimeValue2;
	if( gettimeofday(&TimeValue2,NULL)==-1 )
	{
		printf( "Get time failed,88\n" );
		return;
	}
	double timeuse;
	timeuse = 1000000*(TimeValue2.tv_sec-TimeValue1.tv_sec) +
				TimeValue2.tv_usec-TimeValue1.tv_usec;
	printf( "Time elapsed second:%f\n", timeuse/1000000);
	printf( "Time elapsed micro-second:%f\n", timeuse);
}

void InitDataArea3()
{
	g_mapDataArea3.RemoveAll();
	g_mapDataArea3["First"] = (void *)NULL;  	//1
	g_mapDataArea3["ertetre"] = (void *)NULL;	//2
	g_mapDataArea3["6ytrtuu"] = (void *)NULL;	//3
	g_mapDataArea3["5756h76"] = (void *)NULL;	//4
	g_mapDataArea3["457tg"] = (void *)NULL;	//5
	g_mapDataArea3["GetDName"] = (void *)NULL;	//6
	g_mapDataArea3["erttytt"] = (void *)NULL;	//7
	g_mapDataArea3["adfgf"] = (void *)NULL;	//8
	g_mapDataArea3["ht765"] = (void *)NULL;	//9
	g_mapDataArea3["urt敢"] = (void *)NULL;	//10
	g_mapDataArea3["当然高"] = (void *)NULL;	//11
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -