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