analysis2-2.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <unordered_map>
  5. #include <chrono>
  6. #include <random>
  7. using namespace std;
  8. #include "tire.h"
  9. const int TABLE_SIZE = 5e4;
  10. const int N = 1e5;
  11. const int M1 = 3;
  12. const int M2 = 5;
  13. struct Test {
  14. int num;
  15. string str;
  16. };
  17. vector<long long> umap_times;
  18. vector<long long> tire_times;
  19. vector<string> table;
  20. void umap_test(unordered_map<string, Test>& umap) {
  21. auto start = chrono::high_resolution_clock::now();
  22. for (auto& target : table) {
  23. umap.find(target);
  24. }
  25. auto end = chrono::high_resolution_clock::now();
  26. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  27. umap_times.push_back(duration.count());
  28. }
  29. void tire_test(Tire<Test>& tire) {
  30. auto start = chrono::high_resolution_clock::now();
  31. for (auto& target : table) {
  32. tire.find(target);
  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 = 0, minn = 0x7fffffffffffffff;
  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. Tire<Test> tire;
  58. unordered_map<string, Test> umap;
  59. for (int i = 0; i < TABLE_SIZE; ++i) {
  60. char str[32] = "";
  61. int size = rand() % 30;
  62. for (int j = 0; j < size; ++j) {
  63. str[j] = (char)(rand() % 256);
  64. }
  65. str[size] = '\0';
  66. table.push_back(str);
  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. tire.insert(test.str, test);
  73. };
  74. for (int i = 0; i < N; ++i) {
  75. Test test;
  76. test.num = i + 1;
  77. test.str = to_string(i + 1);
  78. umap.insert({ test.str, test });
  79. };
  80. // group 1
  81. for (int i = 0; i < M1; ++i) {
  82. umap_test(umap);
  83. tire_test(tire);
  84. }
  85. // group 2
  86. for (int i = 0; i < M1; ++i) {
  87. tire_test(tire);
  88. umap_test(umap);
  89. }
  90. // group 3
  91. for (int i = 0; i < M2; ++i) {
  92. umap_test(umap);
  93. }
  94. for (int i = 0; i < M2; ++i) {
  95. tire_test(tire);
  96. }
  97. // group 4
  98. for (int i = 0; i < M2; ++i) {
  99. tire_test(tire);
  100. }
  101. for (int i = 0; i < M2; ++i) {
  102. umap_test(umap);
  103. }
  104. // 分析
  105. print_times("umap", umap_times);
  106. print_times("tire", tire_times);
  107. analysis("umap", umap_times);
  108. analysis("tire", tire_times);
  109. return 0;
  110. }