标签:++ 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