码迷,mamicode.com
首页 > 其他好文 > 详细

基准测试

时间:2020-05-09 22:59:24      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:++   clu   安全   value   rtti   red   alt   mem   图片   

#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)\n";
		
	}
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\n";
	{
		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\n";
	{
		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\n";
	{
		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模式下去运行,查看效率,结果如图
技术图片

多次运行结果一致。

基准测试

标签:++   clu   安全   value   rtti   red   alt   mem   图片   

原文地址:https://www.cnblogs.com/EvansPudding/p/12860477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!