반응형

BLOG main image





..... 


#include <boost/unordered/unordered_map.hpp >


typedef boost::unordered_map<int, std::list<int> > hashListObjectBase_sjh;




int main()

{


hashListObjectBase_sjh con;

con[0].push_back(3);


  //아래와 같이 컨테이너타입을 알아올 수 있다

hashListObjectBase_sjh::value_type::second_type::iterator begin = con[0].begin();

std::cout<<*begin<<std::endl;                        //출력결과는 3이 나온다



return 0;


}





test.cpp




unorderad_map  해쉬구조 탐색하여 값 조사





결과창















http://blog.naver.com/sorkelf/40136119399



unorderd.cpp



해쉬를 사용한 O(1)로 요소액세스 가능한 set과 map


순서에 관해서 특히 보증할수 없는 대신에
std::set이나 std::map보다도 고속으로 요소에 대해 액세스
를 할수 있는 연관 컨테이너이다.

해쉬로 구현되어 있으며
기본적으로 std::set이나 set::map과 같은방식으로 사용할 수 있다
C++표준라이브러리확장안인 TR1에도 이미 해쉬컨테이너가 들어있어
gcc4나 vc9등에서는 사용가능하나
그 이전 환경에서는 사용할수 없는 포터블한 해쉬컨테이너이다

예제





결과)









http://blog.naver.com/chriskr7/60182014718



[C++11] unordered_map performance




짬을 이용해서 이번에 사용했던 놈을 소개할 까 합니다.

std::map하고 performance면에서 차이가 많습니다.

이번에 BMT 관련해서 std::map을 이놈으로 바꾸니 find부분에서 속도가 33%정도 향상되네요.

 

단순히 테스트를 위해 읽을 파일 형성하는 소스입니다.

[-] Collapse

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <memory.h>
using namespace std;

void make_word(char* cbuf){
    int len = rand() % 6 + 1;
    cbuf[len] = 0x00;
    for(int i = 0; i <len; i++)
        cbuf[i] = 'a' + rand() % 26;
}

int main(void){
    char word[7];
    int cnt = 0;

    ofstream out("c:/test/words.txt");

    memset(word, 0x00, 7);
    srand((unsigned)time(NULL));
    while(cnt++ < 10000){
        make_word(word);
        out << word;
        memset(word, 0x00, 7);
        make_word(word);
        out << '\t' << word <<endl;
    }

    out.close();
    return 0;
}

 

실질적으로 unordered_map을 테스트 하는 소스입니다.

[-] Collapse

#include <iostream>

#ifdef __GNUC__
#include <tr1/unordered_map>
using namespace std::tr1;
#else
#include <unordered_map>
#endif

#include <map>
#include <cstdlib>
#include <string>
#include <time.h>
#include <vector>
#include <fstream>
using namespace std;

clock_t start_time, end_time;

typedef map<std::string, std::string> TIntMap;
typedef pair<std::string, std::string> TIntPair;
typedef unordered_map<std::string, std::string> TUnIntMap;

int main()
{
    TIntMap iMap;
    TUnIntMap uMap;
    TIntMap::iterator ip;
    TUnIntMap::iterator up;

    vector<std::pair<std::string, std::string>> v;

    ifstream in("c:/test/words.txt");
    if(!in)
    {
        cerr << "cannot find the file" <<endl;
        exit(EXIT_FAILURE);
    }

    string key, value;
    for(int i = 0; i <10000; i++)
    {
        in >> key >> value;
        v.push_back(std::make_pair(key, value));
    }

    // std::map Insertion
    start_time = clock();
    for(int i = 0; i < v.size(); i++){
        iMap.insert(v[i]);
    }
    end_time = clock();
    cout << "std::map insertion time elapsed : " << ((double)(end_time-start_time)/CLOCKS_PER_SEC) << endl;


    // std::unordered_map Insertion
    start_time = clock();
    for(int i = 0; i < v.size(); i++){
        uMap.insert(v[i]);
    }
    end_time = clock();
    cout << "std::unordered_map insertion time elapsed : " << ((double)(end_time-start_time)/CLOCKS_PER_SEC) << endl;


    srand((unsigned)time(NULL));
    start_time = clock();
    for(int i = 0; i < 1000000; i++)
    {
        string key = v[rand()%10000].first;
        ip = iMap.find(key);
    }
    end_time = clock();
    cout << "std::map find time elapsed : " << ((double)(end_time-start_time)/CLOCKS_PER_SEC) << endl;

    srand((unsigned)time(NULL));
    start_time = clock();
    for(int i = 0; i < 1000000; i++)
    {
        string key = v[rand()%10000].first;
        up = uMap.find(key);
    }
    end_time = clock();
    cout << "std::unorderd_map find time elapsed : " << ((double)(end_time-start_time)/CLOCKS_PER_SEC) << endl;


    in.close();

    return 0;
}

 

결과입니다. 제 PC에서 한거고요.. server에서 했을때도 속도는 거의 2.5~4배 차이가 납니다.


 



반응형

'메타프로그래밍 > Boost::' 카테고리의 다른 글

boost::pool  (0) 2013.05.11
boost::dynamic_bitset  (0) 2013.03.08
boost::bind, boost::mem_fn, boost::ref 조합  (0) 2013.03.01
boost::bind & ref, cref  (0) 2013.02.28
boost::circular_buffer  (0) 2013.02.26

+ Recent posts