fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <unordered_set>
  4. #include <random>
  5. #include <chrono>
  6.  
  7.  
  8. template<typename T>
  9. void fillTheContainerWithInts(T&, int);
  10.  
  11. template<typename T>
  12. void calcFindTime(T const&, int);
  13.  
  14. int main()
  15. {
  16. int const n = 1000000;
  17. std::cout << "set:\n";
  18. std::set<int> s;
  19. fillTheContainerWithInts(s, n);
  20. calcFindTime(s, n);
  21. std::cout << "\nunordered set:\n";
  22. std::unordered_set<int> us;
  23. fillTheContainerWithInts(us, n);
  24. calcFindTime(us, n);
  25.  
  26. return 0;
  27. }
  28.  
  29. template<typename T>
  30. void fillTheContainerWithInts(T& c, int n)
  31. {
  32. for(int i = 0 ; i < n ; ++i)
  33. c.insert(i);
  34. }
  35.  
  36. template<typename T>
  37. void calcFindTime(T const& c, int n)
  38. {
  39. typedef std::chrono::high_resolution_clock Clock;
  40. typedef std::chrono::duration<double> sec;
  41. Clock::time_point t0 = Clock::now();
  42. for(int i = n ; i > 0 ; --i)
  43. c.find(i);
  44.  
  45. Clock::time_point t1 = Clock::now();
  46. std::cout << "Time duration in seconds for " << n << " elements: " << sec(t1 - t0).count() << std::endl;
  47. }
Success #stdin #stdout 0.8s 42512KB
stdin
Standard input is empty
stdout
set:
Time duration in seconds for 1000000 elements: 0.14853

unordered set:
Time duration in seconds for 1000000 elements: 0.0221221