fork(1) download
  1. #include "stdio.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <chrono>
  5. #include <vector>
  6. #include <map>
  7. #include <algorithm>
  8. #include <set>
  9.  
  10. using namespace std::chrono;
  11. using namespace std;
  12.  
  13.  
  14. const vector<string> vecorder({ "srb","jgp","wbz","dhk","ofy","vrw","ybw","sdj","tlf","vut"});
  15. bool veccomp(const string &s1, const string &s2) {
  16. int a = find(vecorder.begin(), vecorder.end(), s1) - vecorder.begin();
  17. int b = find(vecorder.begin(), vecorder.end(), s2) - vecorder.begin();
  18. return a < b;
  19. };
  20.  
  21. vector<string> KeySort(set<string> s, const vector<string>& order){
  22. vector<string> res;
  23. for(const auto& it : order){
  24. auto needle = s.find(it);
  25. if(needle != s.end()){
  26. res.emplace_back(move(*needle));
  27. s.erase(needle);
  28. }
  29. }
  30. for(auto&& it : s) {
  31. res.emplace_back(move(it));
  32. }
  33. return res;
  34. }
  35.  
  36.  
  37. map<const string, const int> maporder({ {"srb",1}, {"jgp",2}, {"wbz",3}, {"dhk",4}, {"ofy",5}, {"vrw",6}, {"ybw",7}, {"sdj",8}, {"tlf",9}, {"vut",10}});
  38. bool mapcomp(const string &s1, const string &s2) {
  39. int a = maporder[s1];
  40. int b = maporder[s2];
  41. //maporder.erase(s1);
  42. return a < b;
  43. };
  44.  
  45. vector<string> vp ={ "srb","jgp","wbz","dhk","ofy","vrw","ybw","sdj","tlf","vut"};
  46. set<string> keyset ={ "srb","jgp","wbz","dhk","ofy","vrw","ybw","sdj","tlf","vut"};
  47.  
  48. void sortWithVector() {
  49. random_shuffle ( vp.begin(), vp.end() );
  50. sort(vp.begin(), vp.end(), veccomp);
  51. }
  52.  
  53. void sortWithMap() {
  54. random_shuffle ( vp.begin(), vp.end() );
  55. sort(vp.begin(), vp.end(), mapcomp);
  56. }
  57.  
  58.  
  59. int main()
  60. {
  61. high_resolution_clock::time_point t1;
  62. high_resolution_clock::time_point t2;
  63. double duration;
  64.  
  65. /*--------------------------------------*/
  66.  
  67. t1 = high_resolution_clock::now();
  68.  
  69. for (int i=0; i < 10000; ++i) sortWithMap();
  70.  
  71. t2 = high_resolution_clock::now();
  72. duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  73. cout<< "map: " <<duration/1000.0 << "ms" << endl;
  74.  
  75. /*--------------------------------------*/
  76.  
  77. t1 = high_resolution_clock::now();
  78.  
  79. for (int i=0; i < 10000; ++i) {random_shuffle ( vp.begin(), vp.end() ); KeySort(keyset, vecorder);}
  80.  
  81. t2 = high_resolution_clock::now();
  82. duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  83. cout<< "AtnNn: " <<duration/1000.0 << "ms" << endl;
  84.  
  85. /*--------------------------------------*/
  86.  
  87. t1 = high_resolution_clock::now();
  88.  
  89. for (int i=0; i < 10000; ++i) sortWithVector();
  90.  
  91. t2 = high_resolution_clock::now();
  92. duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  93. cout << "vector: " << duration/1000.0 << "ms" << endl;
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0.2s 3476KB
stdin
Standard input is empty
stdout
map: 78.921ms
KeySort: 36.968ms
vector: 78.003ms