analysis1.cpp 2.3 KB

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