Advertisement
Guest User

unordered_map mircro-benchmark

a guest
Jul 24th, 2012
1,494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits>
  3. #include <vector>
  4. #include <unordered_map>
  5. #include <map>
  6. #include <chrono>
  7.  
  8. #include <boost/random/mersenne_twister.hpp>
  9. #include <boost/random/uniform_int_distribution.hpp>
  10.  
  11. #include <google/dense_hash_map>
  12. #include <google/sparsehash/libc_allocator_with_realloc.h>
  13.  
  14. #include "concurrent_map.h"
  15.  
  16. static constexpr int SIZE = 10000000;
  17.  
  18. void bench() {
  19.     boost::random::mt19937 rng;
  20.     boost::random::uniform_int_distribution<> dist(std::numeric_limits<uint64_t>::min(), std::numeric_limits<uint64_t>::max());
  21.     std::vector<uint64_t> vec(SIZE);
  22.     for (int i = 0; i < SIZE; ++i) {
  23.         uint64_t val = 0;
  24.         while (val == 0) {
  25.             val = dist(rng);
  26.         }
  27.         vec[i] = val;
  28.     }
  29.     std:unordered_map<int, long double> map;
  30.     //google::dense_hash_map<int, long double> map;
  31.     //map.set_empty_key(0);
  32.     auto begin = std::chrono::high_resolution_clock::now();
  33.     for (int i = 0; i < SIZE; ++i) {
  34.         map[vec[i]] = 0.0;
  35.     }
  36.     auto end = std::chrono::high_resolution_clock::now();
  37.     auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
  38.     std::cout << "inserts: " << elapsed.count() << std::endl;
  39.     std::random_shuffle(vec.begin(), vec.end());
  40.     begin = std::chrono::high_resolution_clock::now();
  41.     long double val;
  42.     for (int i = 0; i < SIZE; ++i) {
  43.         val = map[vec[i]];
  44.     }
  45.     end = std::chrono::high_resolution_clock::now();
  46.     elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
  47.     std::cout << "get: " << elapsed.count() << std::endl;
  48. }
  49.  
  50. int main()
  51. {
  52.     bench();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement