analysis5.cpp 2.4 KB

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