fork(5) download
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4. #include <chrono>
  5.  
  6. static const char CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  7.  
  8. static const int BENCHMARK_SIZE = 10000000;
  9. static const int MAX_STRING_LENGTH = 50;
  10.  
  11. using time_point = std::chrono::high_resolution_clock::time_point;
  12.  
  13. void benchmark(std::vector<std::string> &list)
  14. {
  15. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  16.  
  17. // force a copy of each string in the loop iteration
  18. for (const auto s : list) {
  19. static_cast<void>(s);
  20. }
  21.  
  22. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  23. const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
  24. std::cerr << list[0].length() << ',' << duration << '\n';
  25. }
  26.  
  27.  
  28. void generate_random_string(std::string &str, const int length)
  29. {
  30. static float pi_number = 10 / 7;
  31. for (int i = 0; i < length; ++i) {
  32. str[i] = CHARS[(int)pi_number];
  33. }
  34. pi_number = (pi_number - int(pi_number)) * 10; // gives values of pi on and on.
  35. }
  36.  
  37. int main()
  38. {
  39. std::cerr << "length,time\n";
  40. std::vector<std::string> list;
  41. list.resize(BENCHMARK_SIZE);
  42.  
  43. for (int length = 1; length <= MAX_STRING_LENGTH; length++) {
  44. for (int i = 0; i < BENCHMARK_SIZE; i++) {
  45. list[i].resize(length);
  46. generate_random_string(list[i], length);
  47. }
  48. benchmark(list);
  49. }
  50.  
  51. return 0;
  52. }
  53.  
Time limit exceeded #stdin #stdout #stderr 5s 784588KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
length,time
1,36
2,55
3,56
4,53
5,54
6,55
7,58
8,50
9,48
10,49
11,48
12,51
13,49
14,51
15,49
16,197
17,203
18,200
19,200
20,203