fork(3) download
  1. #include <string>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <chrono>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main(int argc, const char * argv[])
  11. {
  12.  
  13. string s;
  14. for(int i = 0; i < 100000; ++i) s += rand()%26+'A';
  15.  
  16. auto start = chrono::high_resolution_clock::now();
  17. string d;
  18. d.reserve(s.length()*3/2);
  19. for(size_t i = 0; i < s.length(); ++i)
  20. {
  21. d += s[i++];
  22. if (i < s.length())
  23. {
  24. d += s[i];
  25. d += '-';
  26. }
  27. }
  28. auto stop = chrono::high_resolution_clock::now();
  29. cout << chrono::duration_cast<chrono::nanoseconds>(stop-start).count() << endl;
  30.  
  31. start = chrono::high_resolution_clock::now();
  32. string t;
  33. for(size_t i = 0; i < s.length(); ++i)
  34. {
  35. t += s[i++];
  36. if (i < s.length())
  37. {
  38. t += s[i];
  39. t += '-';
  40. }
  41. }
  42. stop = chrono::high_resolution_clock::now();
  43. cout << chrono::duration_cast<chrono::nanoseconds>(stop-start).count() << endl;
  44.  
  45. //cout << t << endl << endl << endl;
  46.  
  47. start = chrono::high_resolution_clock::now();
  48. for (size_t i(0), j(0); i < s.size(); i++, j++)
  49. {
  50. if (j > 1) {
  51.  
  52. s.insert(i, "-");
  53. j = -1;
  54. }
  55. }
  56. stop = chrono::high_resolution_clock::now();
  57. cout << chrono::duration_cast<chrono::nanoseconds>(stop-start).count() << endl;
  58. // cout << s << endl;
  59.  
  60.  
  61. }
  62.  
Success #stdin #stdout 0.51s 3416KB
stdin
Standard input is empty
stdout
965493
1232074
511572171