fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4.  
  5. const int size = 100000000;
  6.  
  7. static char s_str[size];
  8.  
  9. int main()
  10. {
  11. memset(s_str, 'x', size - 1);
  12.  
  13. std::string s(s_str);
  14. clock_t c1, c2, c3, c4;
  15.  
  16. // =
  17. c1 = clock();
  18. for (int i = 0; i < 10; i++) {
  19. s = s_str;
  20. }
  21. c2 = clock();
  22.  
  23. // = std::string
  24. c3 = clock();
  25. for (int i = 0; i < 10; i++) {
  26. s = std::string(s_str);
  27. }
  28. c4 = clock();
  29.  
  30. printf("= : %d\n", static_cast<int>(c2 - c1));
  31. printf("= std::string : %d\n", static_cast<int>(c4 - c3));
  32. }
Success #stdin #stdout 2.39s 100544KB
stdin
Standard input is empty
stdout
=             : 870000
= std::string : 1280000