1
0

analysis3.cpp 2.2 KB

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