CMap與hash map效率對照

2021-09-06 19:27:10 字數 770 閱讀 4324

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 #include #include using namespace std;

#define dataset 99999

#define querynum 9999

int _tmain(int argc, _tchar* argv)

{ srand((unsigned int)time(null));

cmapcmap;

hash_maphmap;

int tempnum[dataset];

int query[querynum];

int exsit = 0;

int j = 0;

for (int i=0; i

HashMap遍歷效率問題

對於hashmap中的key和value的遍歷有兩種方法 1.通過hashmap.keyset 方法返回key的集合,通過遍歷該集合的key得到相應的value值。map map new hashmap iterator iter map.keyset iterator while iter.has...

java foreach與for遍歷效率對比

先看陣列型別的迭代器,拿arraylist來說吧。arraylistlist new arraylist 一般迭代器的寫法 for iterator iterator list.iterator iterator.hasnext for each隱藏迭代器的寫法,避免混亂和出錯的可能,也更簡潔。fo...

c map 對key和value排序

1.對key值排序 使用提供的less grater或自定義排序 include include include include include include std greater using namespace std 按key的長度公升序排列 struct cmpbykeylength in...