analysis4.cpp 2.2 KB

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