1 #include <iostream>
 2 #include <chrono>
 3 #include <array>
 4 
 5 class Timer
 6 {
 7 public:
 8     Timer()
 9     {
10         m_StartTimePoint = std::chrono::high_resolution_clock::now();
11     }
12     
13     ~Timer()
14     {
15         Stop();
16     }
17     
18     void Stop()
19     {
20         auto endTimePoint = std::chrono::high_resolution_clock::now();
21         auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimePoint).time_since_epoch().count();
22         auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count();
23         
24         auto duration = end - start;
25         
26         double ms = duration * 0.001;
27         
28         std::cout << duration << "us (" << ms << "ms)\n";
29     }
30     
31 private:
32     std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint;
33     
34 };
35 
36 int main() {
37     struct Vector3
38     {
39         float x, y, z;
40     };
41     
42     
43     {
44         std::array<std::shared_ptr<Vector3>, 100000> sharedPtr;
45         Timer timer;
46         {
47             for (int i = 0; i < sharedPtr.size(); i++)
48             {
49                 sharedPtr[i] = std::shared_ptr<Vector3>(new Vector3());
50             }
51         }
52     }
53     
54     {
55         std::array<std::shared_ptr<Vector3>, 100000> sharedPtr;
56         {
57             Timer timer;
58             for (int i = 0; i < sharedPtr.size(); i++)
59             {
60                 sharedPtr[i] = std::make_shared<Vector3>();
61             }
62         }
63     }
64     
65     
66     {
67         std::array<std::unique_ptr<Vector3>, 100000> uniquePtr;
68         {
69             Timer timer;
70             for (int i = 0; i < uniquePtr.size(); i++)
71             {
72                 uniquePtr[i] = std::make_unique<Vector3>();
73             }
74         }
75     }
76     
77     return 0;
78 }

 

 posted on 2021-02-16 14:08  何雨龙  阅读(67)  评论(0编辑  收藏  举报