Advertisement
ascendant

maptests

Sep 20th, 2012
1,755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <map>
  2. #include <tuple>
  3. #include <chrono>
  4. #include <vector>
  5. #include <string>
  6. #include <cstdlib>
  7. #include <fstream>
  8. #include <iomanip>
  9. #include <assert.h>
  10. #include <iostream>
  11. #include <unordered_map>
  12.  
  13. using namespace std ;
  14.  
  15. typedef chrono::time_point<chrono::system_clock> SystemTime ;
  16.  
  17. template <typename HashType, typename KeyType> tuple <int, int, int> hash_test(vector <KeyType> &keys)
  18. {
  19. int count(0);
  20. int insertTime(0), fetchTime(0) ;
  21. {
  22. HashType hash ;
  23. {
  24. SystemTime start(chrono::system_clock::now());
  25. for(KeyType &t : keys)
  26. hash [t] = 1;
  27. SystemTime end(chrono::system_clock::now());
  28. insertTime = chrono::duration_cast<chrono::microseconds>(end-start).count();
  29. }
  30. {
  31. SystemTime start(chrono::system_clock::now());
  32. for(KeyType &t : keys)
  33. count += hash [t];
  34. SystemTime end(chrono::system_clock::now());
  35. fetchTime = chrono::duration_cast<chrono::microseconds>(end-start).count();
  36. }
  37. }
  38. //count is used to ensure that the compiler optimisations don't remove the hashing operations
  39. return tuple <int, int, int>(insertTime, fetchTime, count);
  40. }
  41.  
  42. template <typename Key> void maptest(const char *context, vector <Key> &keys)
  43. {
  44. auto uMapTimes = hash_test<unordered_map <Key, int>, Key>(keys);
  45. auto oMapTimes = hash_test<map <Key, int>, Key>(keys);
  46. cout<<context<<endl;
  47. cout<<setw(12)<<"unordered: "<<setw(8)<<get<0>(uMapTimes)<<setw(8)<<get<1>(uMapTimes)<<endl;
  48. cout<<setw(12)<<"ordered: " <<setw(8)<<get<0>(oMapTimes)<<setw(8)<<get<1>(oMapTimes)<<endl;
  49. }
  50.  
  51. int main()
  52. {
  53. std::srand(time(0));
  54.  
  55. enum { KeyCount=1024, MinStrSize=8, MaxStrSize=16 } ;
  56. vector <int> integerKeys(KeyCount);
  57. for(int i=0; i<KeyCount; i++)
  58. integerKeys [i] = i ;
  59.  
  60. vector <string> randomStrings(KeyCount) ;
  61. for(int i=0; i<KeyCount; i++)
  62. {
  63. auto &str = randomStrings[i] ;
  64. str.resize(((MaxStrSize-MinStrSize)%std::rand()) + MinStrSize);
  65. for(char &c : str)
  66. {
  67. c = (('z' - 'a')%std::rand()) + 'a' ;
  68. }
  69. }
  70.  
  71. //wordlist can be downloaded here: http://pastebin.com/yt3s22hf
  72. vector <string> wordKeys ;
  73. ifstream input("wordlist.txt");
  74. assert(input.good());
  75. while(!input.eof())
  76. {
  77. string s ;
  78. input >> s ;
  79. wordKeys.push_back(s);
  80. }
  81. input.close();
  82.  
  83. maptest<int>(" ** Integer Keys ** ", integerKeys);
  84. maptest<string>(" ** Random String Keys ** ", randomStrings);
  85. maptest<string>(" ** Real Words Keys ** ", wordKeys);
  86.  
  87.  
  88. return 0 ;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement