fork(1) download
  1. #include <chrono>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5.  
  6. template <class T>
  7. inline void hash_combine(std::size_t& seed, T const& v)
  8. {
  9. seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  10. }
  11.  
  12. namespace std
  13. {
  14. template<typename T>
  15. struct hash { };
  16. template<typename T>
  17. struct hash<std::vector<T>>
  18. {
  19. typedef std::vector<T> argument_type;
  20. typedef std::size_t result_type;
  21. result_type operator()(argument_type const& in) const
  22. {
  23. size_t size = in.size();
  24. result_type seed = 0;
  25. for (size_t i = 0; i < size; i++)
  26. //Combine the hash of the current vector with the hashes of the previous ones
  27. hash_combine(seed, in[i]);
  28. return seed;
  29. }
  30. };
  31. }
  32.  
  33. int main() {
  34. std::vector<double> arr(1000*1000*10);
  35. auto start = std::chrono::high_resolution_clock::now();
  36. size_t hash = std::hash<std::vector<double>>()(arr);
  37. auto stop = std::chrono::high_resolution_clock::now();
  38. std::cout << "hash of " << arr.size() << " doubles took ";
  39. std::cout << std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
  40. std::cout << " us and resulted in hash: " << hash << std::endl;
  41. return 0;
  42. }
Success #stdin #stdout 0.12s 3460KB
stdin
Standard input is empty
stdout
hash of 10000000 doubles took 51306 us and resulted in hash: 990810500