fork download
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <cstdint>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <set>
  7. #include <iterator>
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <chrono>
  11.  
  12. using namespace std;
  13.  
  14. const auto n = 10000000ULL; // constexptr is tested, no effect
  15.  
  16. int main() {
  17. {
  18. auto t1 = std::chrono::high_resolution_clock::now();
  19.  
  20. std::string s;
  21.  
  22. for (auto i = 0ULL; i < n; i++)
  23. s += "1234567890";
  24.  
  25. auto x = s.length();
  26.  
  27. auto t2 = std::chrono::high_resolution_clock::now();
  28. auto t = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();
  29. cout << "Result: " << x << " -- time (ms):" << t / 0.1e7 << endl;
  30. }
  31.  
  32. auto t1 = std::chrono::high_resolution_clock::now();
  33.  
  34. std::ostringstream ss;
  35.  
  36. for (auto i = 0ULL; i < n; i++)
  37. ss << "1234567890";
  38.  
  39. auto x = ss.str().length();
  40.  
  41. auto t2 = std::chrono::high_resolution_clock::now();
  42. auto t = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();
  43. cout << "Result: " << x << " -- time (ms):" << t / 0.1e7 << endl;
  44. }
Success #stdin #stdout 1.46s 3472KB
stdin
Standard input is empty
stdout
Result: 100000000 -- time (ms):534.02
Result: 100000000 -- time (ms):927.578