analysis2.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <unordered_map>
  5. #include <chrono>
  6. using namespace std;
  7. #include "tire.h"
  8. #include "tire.cpp"
  9. const int N = 1e5;
  10. const int M1 = 3;
  11. const int M2 = 5;
  12. struct Test {
  13. int num;
  14. string str;
  15. };
  16. vector<long long> umap_times;
  17. vector<long long> tire_times;
  18. const string table[16] = { "12345", "99999", "97282", "1", "2","3","7", "2222", "22", "3422", "7482", "9374", "000" };
  19. void umap_test(unordered_map<string, Test>& umap) {
  20. auto start = chrono::high_resolution_clock::now();
  21. for (int j = 0; j < 1e4; ++j) {
  22. for (int i = 0; i < 16; ++i) {
  23. umap.find(table[i]);
  24. }
  25. }
  26. auto end = chrono::high_resolution_clock::now();
  27. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  28. umap_times.push_back(duration.count());
  29. }
  30. void tire_test(Tire<Test>& tire) {
  31. auto start = chrono::high_resolution_clock::now();
  32. for (int j = 0; j < 1e4; ++j) {
  33. for (int i = 0; i < 16; ++i) {
  34. tire.find(table[i]);
  35. }
  36. }
  37. auto end = chrono::high_resolution_clock::now();
  38. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  39. tire_times.push_back(duration.count());
  40. }
  41. void analysis(string name, vector<long long>& nums) {
  42. const int n = nums.size();
  43. long long maxn = LLONG_MIN, minn = LLONG_MAX;
  44. long long sum = 0;
  45. for (long long& num : nums) {
  46. maxn = max(maxn, num);
  47. minn = min(minn, num);
  48. sum += num;
  49. }
  50. cout << name << ": " << "maxn: " << maxn << "ms " << "minn: " << minn << "ms " << "avg: " << (1.0 * sum / n) << "ms " << "sum: " << sum << "ms " << endl;
  51. }
  52. void print_times(string name, vector<long long>& nums) {
  53. cout << name << endl;
  54. for (long long& num : nums) {
  55. cout << num << " ";
  56. };
  57. cout << endl;
  58. }
  59. int main() {
  60. Tire<Test> tire;
  61. unordered_map<string, Test> umap;
  62. for (int i = 0; i < N; ++i) {
  63. Test test;
  64. test.num = i + 1;
  65. test.str = to_string(i + 1);
  66. tire.insert(test.str, test);
  67. };
  68. for (int i = 0; i < N; ++i) {
  69. Test test;
  70. test.num = i + 1;
  71. test.str = to_string(i + 1);
  72. umap.insert({ test.str, test });
  73. };
  74. // group 1
  75. for (int i = 0; i < M1; ++i) {
  76. umap_test(umap);
  77. tire_test(tire);
  78. }
  79. // group 2
  80. for (int i = 0; i < M1; ++i) {
  81. tire_test(tire);
  82. umap_test(umap);
  83. }
  84. // group 3
  85. for (int i = 0; i < M2; ++i) {
  86. umap_test(umap);
  87. }
  88. for (int i = 0; i < M2; ++i) {
  89. tire_test(tire);
  90. }
  91. // group 4
  92. for (int i = 0; i < M2; ++i) {
  93. tire_test(tire);
  94. }
  95. for (int i = 0; i < M2; ++i) {
  96. umap_test(umap);
  97. }
  98. // 分析
  99. print_times("umap", umap_times);
  100. print_times("tire", tire_times);
  101. analysis("umap", umap_times);
  102. analysis("tire", tire_times);
  103. return 0;
  104. }