Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <hash_map>
  2. #include <map>
  3. #include <stdio.h>
  4. #include <ctime>
  5. #include <iostream>
  6.  
  7. enum {
  8.     TEST_TYPE_SIZE = 6,
  9.     TEST_DATASET_SIZE = 5000000,
  10.     EXPERIMENT_SIZE= 1,
  11. };
  12.  
  13. struct some_type {
  14.     enum {SIZE = TEST_TYPE_SIZE};
  15.     int data[SIZE];
  16.     int tag_value;
  17. };
  18.  
  19. typedef std::map<char*, some_type> map_t;
  20. typedef std::hash_map<char*, some_type> hashmap_t;
  21.  
  22.  
  23. int main()
  24. {
  25.     std::list<some_type*> test_data;
  26.     std::list<char*> trashcan;
  27.     typedef std::list<char*>::iterator tit;    
  28.    
  29.     for (size_t i = 0; i < TEST_DATASET_SIZE; ++i) {
  30.         test_data.push_back(new some_type);
  31.         trashcan.push_back(new char[rand()%100]);
  32.     }
  33.  
  34.     //////////////////////////////////////////////////////////////////////////
  35.     // map::insert
  36.     map_t test_maps[EXPERIMENT_SIZE];
  37.     std::clock_t start = std::clock();
  38.     for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
  39.     {      
  40.         map_t& m = test_maps[i];
  41.         for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
  42.             some_type v;
  43.             v.tag_value = rand();
  44.             *(int*)v.data = v.tag_value;
  45.             m.insert(std::make_pair(*it, v));
  46.         }
  47.     }
  48.     std::clock_t end = std::clock();
  49.  
  50.     size_t dummy_val = 0;
  51.     for(size_t i = 0; i < EXPERIMENT_SIZE; ++i) {
  52.         map_t& m = test_maps[i];
  53.         typedef map_t::iterator iter;
  54.         for( iter it = m.begin(); it != m.end(); ++it) {
  55.             dummy_val += it->second.data[1];
  56.         }
  57.     }
  58.  
  59.     std::clock_t map_insert_result = (end - start);
  60.  
  61.     std::cout << "std::map insert score: " << map_insert_result << " (" << dummy_val << ")" << std::endl;
  62.     //////////////////////////////////////////////////////////////////////////
  63.  
  64.     hashmap_t test_hashmaps[EXPERIMENT_SIZE];
  65.     start = std::clock();
  66.     for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
  67.     {      
  68.         hashmap_t& m = test_hashmaps[i];
  69.         for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
  70.             some_type v;
  71.             v.tag_value = rand();
  72.             *(int*)v.data = v.tag_value;
  73.             m.insert(std::make_pair(*it, v));
  74.         }
  75.     }
  76.     end = std::clock();
  77.  
  78.     dummy_val = 0;
  79.     for(size_t i = 0; i < EXPERIMENT_SIZE; ++i) {
  80.         hashmap_t& m = test_hashmaps[i];
  81.         typedef hashmap_t::iterator iter;
  82.         for( iter it = m.begin(); it != m.end(); ++it) {
  83.             dummy_val += it->second.data[1];
  84.         }
  85.     }
  86.  
  87.     std::clock_t hashmap_insert_result = (end - start);
  88.     std::cout << "std::hash_map insert score: " << hashmap_insert_result << " (" << dummy_val << ")" << std::endl;
  89.     //////////////////////////////////////////////////////////////////////////
  90.     dummy_val = 0;
  91.     start = std::clock();
  92.     for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
  93.         for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
  94.         {      
  95.             map_t& m = test_maps[i];
  96.             map_t::iterator res = m.find(*it);
  97.  
  98.             if (res != m.end())
  99.                 dummy_val += res->second.data[3];
  100.         }
  101.     }
  102.     end = std::clock();
  103.     std::clock_t map_search_result = (end - start);
  104.     std::cout << "std::map search score: " << map_search_result << " (" << dummy_val << ")" << std::endl;
  105.     //////////////////////////////////////////////////////////////////////////
  106.     dummy_val = 0;
  107.     start = std::clock();
  108.     for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
  109.         for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
  110.         {      
  111.             hashmap_t& m = test_hashmaps[i];
  112.             hashmap_t::iterator res = m.find(*it);
  113.  
  114.             if (res != m.end())
  115.                 dummy_val += res->second.data[3];
  116.         }
  117.     }
  118.     end = std::clock();
  119.     std::clock_t hashmap_search_result = (end - start);
  120.     std::cout << "std::hash_map search score: " << hashmap_search_result << " (" << dummy_val << ")" << std::endl;
  121.  
  122.     return 0;
  123. }
  124.