erase_example.cpp 2.6 KB

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