基准测试 in C++【C++学习笔记】

基准测试 in C++【C++学习笔记】,第1张

74.基准测试 in C++

你写了一段新代码,像把它的性能和你过去做的方法做一个比较,看看谁更快。而在C++中则有很多不同的方式来实现。

如此,如下讨论的便是如何实际测量C++代码的性能(Cherno的观点和方法论)

#include 
#include 

int main() {
    int val = 0;
    for (int i = 0; i < 1000000; i++) {
        val += 2;
    }
    std::cout << val << std::endl;
}

如上的代码,如何测量其性能呢?

可用的简单方法是:创建一个简单的,有作用域的计时(“简单”)

#include 
#include 
#include 	//计时工具
#include 

class Timer {
public:  
    Timer() {
        std::chrono::high_resolution_clock::now();
    }
    ~Timer() {
        Stop();
    }
    void Stop() {
        auto endTimePoint = std::chrono::high_resolution_clock::now();
        auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimePoint).time_since_epoch().count();
        //microseconds意思是将数据转换为微秒(很多时候测量的东西可能会小于一毫秒,则这样就会得到无用的数据)
        //time_since_epoch()是自时间起始点到现在的时长
        //count就是计数了
        //注意这里返回的类型是long long,即是这里的auto便是longlong
         auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count();
        auto duration = end - start;
        //这里再进一步转换为毫秒数,就直接乘0.001就可以了,移动小数点的事情
        double ms = durtion * 0.001;
        
        std::cout << ms << "ms\n"; 
    }
private:
    std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint;
};

//上面的计时工具没看懂没关系,只需要知道是根据作用域范围来计时的就好了
int main() {
    int val = 0;
    //所以只需要圈起来一个作用域,就可以计时里面的时间了,可太秒了
    {
        Timer timer;	//就创建一个实体就好了,它将在整个作用域范围内持续计时
        for (int i = 0; i < 1000000; i++) {
            val += 2;
        }
    }
    std::cout << val << std::endl;
}

这便是一个很好用,常用的方法了

下面再写一个关于比较shared_ptrunique_ptr的速度

#include 
#include 
#include 	//计时工具

class Timer {
public:  
    Timer() {
        std::chrono::high_resolution_clock::now();
    }
    ~Timer() {
        Stop();
    }
    void Stop() {
        auto endTimePoint = std::chrono::high_resolution_clock::now();
        auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimePoint).time_since_epoch().count();
         auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count();
        auto duration = end - start;
        double ms = durtion * 0.001;
        std::cout << ms << "ms\n"; 
    }
private:
    std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint;
};

int main() {
    struct Vector2 {
        float x, y;
    };
    //对照体
    {
        Timer timer;
        std::array<std::shared_ptr<Vector2>, 1000> sharedPtrs;
        for (int i = 0; i < sharedPtrs.size(); i++) {
            sharedPtrs[i] = std::make_shared<Vector2>();
        }
    }
    //是先比较make_shared和new给智能指针赋值的 *** 作
    {
        Timer timer;
        std::array<std::shared_ptr<Vector2>, 1000> sharedPtrs;
        for (int i = 0; i < sharedPtrs.size(); i++) {
            sharedPtrs[i] = std::shared_ptr<Vector2>(new Vector2);
        }
    }
    //比较创造shared更快还是unique更快
    {
        Timer timer;
        std::array<std::unique_ptr<Vector2>, 1000> sharedPtrs;
        for (int i = 0; i < sharedPtrs.size(); i++) {
            sharedPtrs[i] = std::unique_ptr<Vector2>(new Vector2);
        }
    }
}

可以用这般来测量其速度

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/872786.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存