1
0

destruct_example.cpp 2.3 KB

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