fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <iterator>
  4. #include <vector>
  5. #include <set>
  6. #include <ctime>
  7. #include <algorithm>
  8.  
  9. int main()
  10. {
  11. using namespace std;
  12.  
  13. vector<string> vs(200000);
  14. for (int i = 0; i < vs.size(); ++i)
  15. {
  16. vs[i] = to_string(i);
  17. }
  18.  
  19. set<string> ss(vs.begin(), vs.end());
  20.  
  21. {
  22. cout << "linear search on vector" << endl;
  23. time_t now = clock();
  24. auto s = find(vs.begin(), vs.end(), "100000");
  25. cout << "linear search for \"100000\" took: "
  26. << double(clock() - now) / CLOCKS_PER_SEC << " found " << *s << endl;
  27. }
  28. {
  29. cout << "\nbinary search on vector" << endl;
  30. sort(vs.begin(), vs.end());
  31. time_t now = clock();
  32. auto s = binary_search(vs.begin(), vs.end(), "100000");
  33. cout << "binary search for \"100000\" took: "
  34. << double(clock() - now) / CLOCKS_PER_SEC << " found " <<boolalpha << s << endl;
  35. }
  36. {
  37. cout << "\nbinary search on set" << endl;
  38. time_t now = clock();
  39. auto s = ss.find("100000");
  40. cout << "binary search for \"100000\" took: "
  41. << double(clock() - now) / CLOCKS_PER_SEC << " found " << *s << endl;
  42. }
  43. }
Success #stdin #stdout 0.35s 12856KB
stdin
Standard input is empty
stdout
linear search on vector
linear search for "100000" took: 0 found 100000

binary search on vector
binary search for "100000" took: 0 found true

binary search on set
binary search for "100000" took: 0 found 100000