fork(4) download
  1. #include <cstdio>
  2. #include <string.h>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. using std::string;
  8.  
  9. struct Timestamper {
  10. clock_t start;
  11.  
  12. Timestamper() : start(clock()) {}
  13. ~Timestamper() {
  14. printf("Time=%.3fs\n", (clock() - start) * 1.0 / CLOCKS_PER_SEC);
  15. }
  16. };
  17.  
  18. int len;
  19.  
  20. template<typename T>
  21. void calc1(T s) {
  22. Timestamper t;
  23. int ans = 0;
  24. for (int i = 0; i < len ; i++)
  25. for (int j = 0; j < len ; j++)
  26. ans += s[i] == s[j];
  27. printf("%d\n", ans);
  28. }
  29.  
  30. template<typename T>
  31. void calc2(T s) {
  32. Timestamper t;
  33. int ans = 0;
  34. for (size_t i = 0; i < s.length(); i++)
  35. for (size_t j = 0; j < s.length(); j++)
  36. ans += s[i] == s[j];
  37. printf("%d\n", ans);
  38. }
  39.  
  40. const int MAXL = int(1.5e4);
  41. char s[MAXL + 1];
  42.  
  43. int main() {
  44. srand(123456);
  45. for (int i = 0; i < MAXL; i++)
  46. s[i] = 'a' + (rand() % 26);
  47. s[MAXL] = 0;
  48. len=MAXL;
  49. calc1<const char*>(s);
  50. calc1< char*>(s);
  51.  
  52. string str(s);
  53. calc2<const string&>(str);
  54. calc2<const string >(str);
  55. calc2< string >(str);
  56. calc2< string >(std::move(str));
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 2.29s 3488KB
stdin
Standard input is empty
stdout
8675070
Time=0.240s
8675070
Time=0.240s
8675070
Time=0.240s
8675070
Time=0.240s
8675070
Time=0.660s
8675070
Time=0.670s