• CMap与hash_map效率对照


    CMap与hash_map底层均採用hash stable实现,CMap是MFC提供的模板类。hash_map尽管眼下并未纳入C++标准模板类库,但差点儿每一个版本号的STL都提供了对应的实现。CMap与hash_map的存储于查询效率比較例如以下:

    利用rand函数随机生成99999个整数构成查询数据集,紧接着申请9999个整数作为查询。測试两个模板类的插入与查询总时间,測试结果显示:当查询都不存在时CMap时间大约16ms,hash_map为0ms;当大部分查询存在时CMap时间为624,而hash_map平均为5ms。当key为字符串时,hash_map比CMap快,速度为CMap的近11倍。由于全部的測试都是随机产生的,因此以上的时间均为平均时间。

    參考代码:

    #include "stdafx.h"
    #include "afxtempl.h"
    #include <hash_map>
    #include <time.h>
    #include <iostream>
    using namespace std;
    
    #define DATASET 99999
    #define QUERYNUM 9999
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	srand((unsigned int)time(NULL));
    	CMap<int, int, int, int> cmap;
    	hash_map<int, int> hmap;
    	int tempnum[DATASET];
    	int query[QUERYNUM];
    	int exsit = 0;
    	int j = 0;
    
    	for (int i=0; i<DATASET; ++i)
    	{
    //		tempnum[i] = rand()%1000;
    		tempnum[i] = rand();
    	}
    
    	for (int i=0; i<QUERYNUM; ++i)
    	{
    //		query[i] = rand() + 1001;
    		query[i] = rand();
    	}
    
    	
    	DWORD start;
    	start = GetTickCount();
    	for (int i=0; i<DATASET; ++i)
    	{
    		cmap[tempnum[i]] = 1;
    	}
    
    	for (int i=0; i<QUERYNUM; ++i)
    	{
    		if (cmap.Lookup(query[i], j))
    		{
    			exsit++;
    		}
    	}
    	cmap.RemoveAll();
    	cout<<"Query Time"<<(double)(GetTickCount() - start)<<" :"<<exsit<<endl;
    
    	exsit = 0;
    	start = GetTickCount();
    	for (int i=0; i<DATASET; ++i)
    	{
    		hmap[tempnum[i]] = 1;
    	}
    
    	for (int i=0; i<QUERYNUM; ++i)
    	{
    		if (hmap.find(query[i]) != hmap.end())
    		{
    			exsit++;
    		}
    	}
    	hmap.clear();
    	cout<<"Query Time"<<(double)(GetTickCount() - start)<<" :"<<exsit<<endl;
    
    	system("pause");
    	return 0;
    }
    


  • 相关阅读:
    pyhton 163 email ssl attach file
    Database creation error: relation "ir_model" does not exist LINE 1: SELECT * FROM ir_model WHERE state='manual' ^
    爬虫心得
    WSL windows子系统ubuntu18.04建设自己的乌云
    WSL windwos 子系统 ubuntu18.04安装mysql
    python 163 email 554
    自定义的应用层协议(转)
    嵌入式杂谈之文件系统(转)
    linux VFS
    C++之保护和私有构造函数与析构函数
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4083932.html
Copyright © 2020-2023  润新知