1
0

destruct_example.cpp 2.2 KB

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