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. const int N = 1e5;
  10. const int M1 = 3;
  11. const int M2 = 5;
  12. struct Test {
  13. int num;
  14. string str;
  15. };
  16. vector<long long> umap_times;
  17. vector<long long> tire_times;
  18. void umap_test(unordered_map<string, Test>& umap) {
  19. auto start = chrono::high_resolution_clock::now();
  20. for (auto& [_, item] : umap) {
  21. }
  22. auto end = chrono::high_resolution_clock::now();
  23. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  24. umap_times.push_back(duration.count());
  25. }
  26. void tire_test(Tire<Test>& tire) {
  27. auto start = chrono::high_resolution_clock::now();
  28. for (auto& item : tire) {
  29. }
  30. auto end = chrono::high_resolution_clock::now();
  31. auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
  32. tire_times.push_back(duration.count());
  33. }
  34. void analysis(string name, vector<long long>& nums) {
  35. const int n = nums.size();
  36. long long maxn = LLONG_MIN, minn = LLONG_MAX;
  37. long long sum = 0;
  38. for (long long& num : nums) {
  39. maxn = max(maxn, num);
  40. minn = min(minn, num);
  41. sum += num;
  42. }
  43. cout << name << ": " << "maxn: " << maxn << "ms " << "minn: " << minn << "ms " << "avg: " << (1.0 * sum / n) << "ms " << "sum: " << sum << "ms " << endl;
  44. }
  45. void print_times(string name, vector<long long>& nums) {
  46. cout << name << endl;
  47. for (long long& num : nums) {
  48. cout << num << " ";
  49. };
  50. cout << endl;
  51. }
  52. int main() {
  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. }