analysis2.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 num;
  13. string str;
  14. };
  15. vector<long long> umap_times;
  16. vector<long long> tire_times;
  17. const string table[16] = { "12345", "99999", "97282", "1", "2","3","7", "2222", "22", "3422", "7482", "9374", "000" };
  18. void umap_test(unordered_map<string, Test>& umap) {
  19. auto start = chrono::high_resolution_clock::now();
  20. for (int j = 0; j < 1e4; ++j) {
  21. for (int i = 0; i < 16; ++i) {
  22. umap.find(table[i]);
  23. }
  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 (int j = 0; j < 1e4; ++j) {
  32. for (int i = 0; i < 16; ++i) {
  33. tire.find(table[i]);
  34. }
  35. }
  36. auto end = chrono::high_resolution_clock::now();
  37. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  38. tire_times.push_back(duration.count());
  39. }
  40. void analysis(string name, vector<long long>& nums) {
  41. const int n = nums.size();
  42. long long maxn = LLONG_MIN, minn = LLONG_MAX;
  43. long long sum = 0;
  44. for (long long& num : nums) {
  45. maxn = max(maxn, num);
  46. minn = min(minn, num);
  47. sum += num;
  48. }
  49. cout << name << ": " << "maxn: " << maxn << "ms " << "minn: " << minn << "ms " << "avg: " << (1.0 * sum / n) << "ms " << "sum: " << sum << "ms " << endl;
  50. }
  51. void print_times(string name, vector<long long>& nums) {
  52. cout << name << endl;
  53. for (long long& num : nums) {
  54. cout << num << " ";
  55. };
  56. cout << endl;
  57. }
  58. int main() {
  59. Tire<Test> tire;
  60. unordered_map<string, Test> umap;
  61. for (int i = 0; i < N; ++i) {
  62. Test test;
  63. test.num = i + 1;
  64. test.str = to_string(i + 1);
  65. tire.insert(test.str, test);
  66. };
  67. for (int i = 0; i < N; ++i) {
  68. Test test;
  69. test.num = i + 1;
  70. test.str = to_string(i + 1);
  71. umap.insert({ test.str, test });
  72. };
  73. // group 1
  74. for (int i = 0; i < M1; ++i) {
  75. umap_test(umap);
  76. tire_test(tire);
  77. }
  78. // group 2
  79. for (int i = 0; i < M1; ++i) {
  80. tire_test(tire);
  81. umap_test(umap);
  82. }
  83. // group 3
  84. for (int i = 0; i < M2; ++i) {
  85. umap_test(umap);
  86. }
  87. for (int i = 0; i < M2; ++i) {
  88. tire_test(tire);
  89. }
  90. // group 4
  91. for (int i = 0; i < M2; ++i) {
  92. tire_test(tire);
  93. }
  94. for (int i = 0; i < M2; ++i) {
  95. umap_test(umap);
  96. }
  97. // 分析
  98. print_times("umap", umap_times);
  99. print_times("tire", tire_times);
  100. analysis("umap", umap_times);
  101. analysis("tire", tire_times);
  102. return 0;
  103. }