您的位置:首页 > 移动开发 > IOS开发

vc的hash_map怎么啦

2006-05-16 12:35 162 查看
以下比较均基于 release 版本,debug 有调试信息不能作为标准。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <hash_map>
#include <afxtempl.h>
#include <atlstr.h>
using namespace std;
#pragma warning(disable : 4996)

int _tmain(int argc, _TCHAR* argv[])
{
    srand((unsigned)time(0));
    char str[50];
    map<string,int> smap;
    map<string,int>::value_type val_type;
    hash_map<string,int> hmap;
    CMap<CString,LPCTSTR,int,int&> cmap(2221);

    for(int i=0;i<2048;i++)
    {
        int temp = rand();
        sprintf(str,"%d%d%",temp,temp);
        smap[str] = i;
        hmap[str] = i;
        cmap[str] = i;
    }

    map<string,int>::iterator it;
    for(it=smap.begin(),i=0;it!=smap.end()&&i<1000;it++,i++)
    {
        //cout<<it->first<<" "<<it->second<<endl;

    }
   
    LARGE_INTEGER c1,c2;
    QueryPerformanceCounter(&c1);
    smap.find(it->first);

    hash_map<string,int>::iterator it11;
    QueryPerformanceCounter(&c2);
    cout<<c2.QuadPart-c1.QuadPart<<endl;
    QueryPerformanceCounter(&c1);
    it11 = hmap.find(it->first);
    QueryPerformanceCounter(&c2);
    cout<<c2.QuadPart-c1.QuadPart<<endl;
    CString cs = it->first.c_str();
    QueryPerformanceCounter(&c1);
    cmap.Lookup(cs,i);
    QueryPerformanceCounter(&c2);
    cout<<c2.QuadPart-c1.QuadPart<<endl;
 
    cout<<it11->first<<" "<<it11->second<<endl;
   
    return 0;
}
对于sgi stl    map hash_map  和 mfc 的cmap 执行时间如下: 测试环境vc2003
map              5000-6000 左右
hash_map  3000-4550 左右
cmap            4000-11844 左右
但是当使用vc hash_map 的时候居然经常在20000 以上, 差距也太大了, 而且要声明hash函数和比较函数也麻烦,要生成一个如下相似的类, 跟sgi stl 差异很大。
template<class _Kty,
class _Pr = less<_Kty> >
class hash_compare
{   
public:
    enum
    {   
        bucket_size = 8,   
        min_buckets = 16
    };   
    hash_compare()
            : comp()
    {  
    }
     hash_compare(_Pr _Pred)
            : comp(_Pred)
    {    // construct with _Pred comparator
     }
     size_t operator()(const _Kty& _Keyval) const
     {    // hash _Keyval to size_t value
            return ((size_t)hash_value(_Keyval));
     }
     bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const
     {    // test if _Keyval1 ordered before _Keyval2
            return (comp(_Keyval1, _Keyval2));
     }
protected:
    _Pr comp;
};
如果使用了较长字符串做key,数据量很大,不需要排序使用hash_map比map要有效率(hash 函数不能太差),上面的例子因为生成的都是随机数字,相对文本字符串比较次数要多一些,开头只有1-9 9中情况,不像文本有26个字符,算上大小写数字就更多了, 所以map本可以表现得更好一点.
不过如果是vc的话,最好还是移植sgi 的hash_map 或者用mfc的cmap . 而且在msdn中: 如果您在 std 命名空间中使用 <hash_map> 和 <hash_set> 头文件的成员,将会显示 C4996.在未来版本中可能不再支持此函数。

或许那里用的不对,但如此基本操作不应该有成倍的差距

现在又到快速排序了,对于sort() 函数,sgi版本 稍快于普通快速排序,远远大于vc 自己身的快速排序(或许称不上快速排序),有2倍多的差距阿. 看来开源就是好啊。sigh 还是装个stlport , 跨平台也省事。

最简单的快速排序
int partation(int* a,int l,int r)
{
    int i=l-1,j=r,v=a[r];
    for(;;)
    {
          while(a[++i] < v);
          while(a[--j] > v) if(j<=i) break;
          if(j<=i) break;
          swap(a[i],a[j]);
    }
    swap(a[i],a[r]);
    return i;
}

void qsort(int* a,int l,int r)
{
    while(l<r)
    {
         // sig stl 为了防止分割恶化, 相当于多了一段。所以速度略好于普通快排
         //  if(r-l<32)
         // {
         //       insertsort(a,l,r);
        //        return;
        //  }
        int i = partation(a,l,r);
         qsort(a,l,i-1);
          l = i+1;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息