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. struct Test {
  10. int num;
  11. string str;
  12. };
  13. vector<long long> umap_times;
  14. vector<long long> tire_times;
  15. const string table[16] = { "12345", "99999", "97282", "1", "2","3","7", "2222", "22", "3422", "7482", "9374", "000" };
  16. void umap_test(unordered_map<string, Test>& umap) {
  17. auto start = chrono::high_resolution_clock::now();
  18. for (int j = 0; j < 1e4; ++j) {
  19. for (int i = 0; i < 16; ++i) {
  20. umap.find(table[i]);
  21. }
  22. }
  23. auto end = chrono::high_resolution_clock::now();
  24. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  25. umap_times.push_back(duration.count());
  26. }
  27. void tire_test(Tire<Test>& tire) {
  28. auto start = chrono::high_resolution_clock::now();
  29. for (int j = 0; j < 1e4; ++j) {
  30. for (int i = 0; i < 16; ++i) {
  31. tire.find(table[i]);
  32. }
  33. }
  34. auto end = chrono::high_resolution_clock::now();
  35. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  36. tire_times.push_back(duration.count());
  37. }
  38. void analysis(string name, vector<long long>& nums) {
  39. const int n = nums.size();
  40. long long maxn = LLONG_MIN, minn = LLONG_MAX;
  41. long long sum = 0;
  42. for (long long& num : nums) {
  43. maxn = max(maxn, num);
  44. minn = min(minn, num);
  45. sum += num;
  46. }
  47. cout << name << ": " << "maxn: " << maxn << "ms " << "minn: " << minn << "ms " << "avg: " << (1.0 * sum / n) << "ms " << "sum: " << sum << "ms " << endl;
  48. }
  49. void print_times(string name, vector<long long>& nums) {
  50. cout << name << endl;
  51. for (long long& num : nums) {
  52. cout << num << " ";
  53. };
  54. cout << endl;
  55. }
  56. int main() {
  57. const int N = 1e6;
  58. const int M1 = 3;
  59. const int M2 = 5;
  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. }