analysis3.cpp 2.2 KB

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