1
0

erase_example.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const int N = 1e5;
  9. const int M = 5e4;
  10. const int M1 = 3;
  11. const int M2 = 5;
  12. struct Test {
  13. int a;
  14. short b;
  15. int c;
  16. };
  17. vector<long long> umap_times;
  18. vector<long long> tire_times;
  19. vector<string> data_table;
  20. void init_data_table() {
  21. for (int i = 0; i < N; ++i) {
  22. char str[32] = "";
  23. int size = rand() % 30;
  24. for (int j = 0; j < size; ++j) {
  25. str[j] = (char)(rand() % 256);
  26. }
  27. str[size] = '\0';
  28. data_table.push_back(str);
  29. }
  30. }
  31. vector<string> target_table;
  32. void init_target_table() {
  33. for (int i = 0; i < M; ++i) {
  34. char str[32] = "";
  35. int size = rand() % 30;
  36. for (int j = 0; j < size; ++j) {
  37. str[j] = (char)(rand() % 256);
  38. }
  39. str[size] = '\0';
  40. target_table.push_back(str);
  41. }
  42. }
  43. void umap_test() {
  44. const int n = data_table.size();
  45. unordered_map<string, Test> umap;
  46. for (int i = 0; i < n; ++i) {
  47. Test t;
  48. t.a = i;
  49. t.c = n + i;
  50. umap.insert({ data_table[i], t });
  51. }
  52. auto start = chrono::high_resolution_clock::now();
  53. for (auto& target : target_table) {
  54. umap.erase(target);
  55. }
  56. auto end = chrono::high_resolution_clock::now();
  57. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  58. umap_times.push_back(duration.count());
  59. }
  60. void tire_test() {
  61. const int n = data_table.size();
  62. Tire<Test> tire;
  63. for (int i = 0; i < n; ++i) {
  64. Test t;
  65. t.a = i;
  66. t.c = n + i;
  67. tire.insert(data_table[i], t);
  68. }
  69. auto start = chrono::high_resolution_clock::now();
  70. for (auto& target : target_table) {
  71. tire.erase(target);
  72. }
  73. auto end = chrono::high_resolution_clock::now();
  74. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  75. tire_times.push_back(duration.count());
  76. }
  77. void analysis(string name, vector<long long>& nums) {
  78. const int n = nums.size();
  79. long long maxn = LLONG_MIN, minn = LLONG_MAX;
  80. long long sum = 0;
  81. for (long long& num : nums) {
  82. maxn = max(maxn, num);
  83. minn = min(minn, num);
  84. sum += num;
  85. }
  86. cout << name << ": " << "maxn: " << maxn << "ms " << "minn: " << minn << "ms " << "avg: " << (1.0 * sum / n) << "ms " << "sum: " << sum << "ms " << endl;
  87. }
  88. int main() {
  89. init_data_table();
  90. init_target_table();
  91. // group 1
  92. for (int i = 0; i < M1; ++i) {
  93. umap_test();
  94. tire_test();
  95. }
  96. // group 2
  97. for (int i = 0; i < M1; ++i) {
  98. tire_test();
  99. umap_test();
  100. }
  101. // group 3
  102. for (int i = 0; i < M2; ++i) {
  103. umap_test();
  104. }
  105. for (int i = 0; i < M2; ++i) {
  106. tire_test();
  107. }
  108. // group 4
  109. for (int i = 0; i < M2; ++i) {
  110. tire_test();
  111. }
  112. for (int i = 0; i < M2; ++i) {
  113. umap_test();
  114. }
  115. // 分析
  116. analysis("umap", umap_times);
  117. analysis("tire", tire_times);
  118. return 0;
  119. }