analysis4.cpp 2.3 KB

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