zoukankan      html  css  js  c++  java
  • 基准测试

    #include <iostream>
    #include <memory>
    #include <chrono> 
    
    class Timer
    {
    public:
    	Timer()
    	{
    		m_StartTimepoint = 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 = duration * 0.001;
    
    		std::cout << duration << "us (" << ms << "ms)
    ";
    		
    	}
    private:
    	std::chrono::time_point< std::chrono::high_resolution_clock> m_StartTimepoint;
    };
    
    
    int main()
    {
    
    	int value = 0;
    	{
    		Timer timer;
    
    		for (int i = 0; i < 10000; i++)
    			value += 2;
    	}
    	 
    	std::cout << value << std::endl;
    
    	__debugbreak(); //windows api 断点
    }
    

    //智能指针查看
    int main()
    {
    	struct Vector2
    	{
    		float x, y;
    	};
    
    	std::cout << "make share
    ";
    	{
    		std::array<std::shared_ptr<Vector2>, 1000> sharedPtrs;
    		Timer timer;
    		for (int i = 0; i < sharedPtrs.size(); i++)
    			sharedPtrs[i] = std::make_shared<Vector2>();
    	}
    
    	std::cout << "new share
    ";
    	{
    		std::array<std::shared_ptr<Vector2>, 1000> sharedPtrs;
    		Timer timer;
    		for (int i = 0; i < sharedPtrs.size(); i++)
    			sharedPtrs[i] = std::shared_ptr<Vector2>(new Vector2());
    	}
    
    	std::cout << "make unique
    ";
    	{
    		std::array<std::unique_ptr<Vector2>, 1000> sharedPtrs;
    		Timer timer;
    		for (int i = 0; i < sharedPtrs.size(); i++)
    			sharedPtrs[i] = std::make_unique<Vector2>();
    	}
    }
    

    debug模式下会做非常多安全性的工作,所以我们改成release模式下去运行,查看效率,结果如图

    多次运行结果一致。

  • 相关阅读:
    Openvswitch手册(6): QoS
    [转]linux VLAN配置(vconfig)
    [转]Understand QoS at OpenSwitch
    Openvswitch手册(5): VLAN and Bonding
    Openvswitch手册(4): Mirror
    Openvswitch手册(3): sFlow, netFlow
    Mysql主从复制(重置版)
    Mysql备份工具Xtrabackup
    Mysql用户管理
    Mysql备份工具mysqldump和mysqlhotcopy
  • 原文地址:https://www.cnblogs.com/EvansPudding/p/12860477.html
Copyright © 2011-2022 走看看