zoukankan      html  css  js  c++  java
  • C++计时器测试代码运行时间

     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::milliseconds>(m_StartTimePoint).time_since_epoch().count();
    22         auto end = std::chrono::time_point_cast<std::chrono::milliseconds>(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)
    ";
    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 }
  • 相关阅读:
    django-restframework使用
    django-xadmin使用
    python2.7.5升级到2.7.14或者直接升级到3.6.4
    mysql-5.7.25安装以及使用
    django1.9安装以及使用
    Algorithm negotiation failed
    ELK的搭建以及使用
    python 3.5 成功安装 scrapy 的步骤
    pandas基础-Python3
    C#命名规则和编码规范
  • 原文地址:https://www.cnblogs.com/noluye/p/14406600.html
Copyright © 2011-2022 走看看