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模式下去运行,查看效率,结果如图

    多次运行结果一致。

  • 相关阅读:
    C语言——enum
    C语言——杂实例
    pc上用C语言模拟51多任务的案例程序
    C语言结构体实例-创建兔子
    C语句模拟多任务实例
    求解1^2+2^2+3^2+4^2+...+n^2的方法(求解1平方加2平方加3平方...加n平方的和)
    啊啊啊哦哦
    2013多校联合3 G The Unsolvable Problem(hdu 4627)
    c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
    揭开Socket编程的面纱
  • 原文地址:https://www.cnblogs.com/EvansPudding/p/12860477.html
Copyright © 2011-2022 走看看