C语言学习笔记45中,如何进行可视化基准测试的是?
摘要:代码 #include <iostream> #include <string> #include <chrono&
代码
#include <iostream>
#include <string>
#include <chrono>
#include <cmath>
class Timer
{
public:
Timer(const char* name)
: m_Name(name), m_Stopped(false)
{
m_StartTimepoint = std::chrono::high_resolution_clock::now();
}
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 << m_Name << ": " << duration << "μs (" << ms << "ms)\n";
m_Stopped = true;
}
~Timer()
{
if(!m_Stopped)
Stop();
}
private:
const char* m_Name; // 计时器的名字
bool m_Stopped;
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint;
};
// 需要测试性能的函数
void Function1()
{
Timer timer("Function1");
for (int i = 0 ; i < 1000; i++)
std::cout << "Hello World!" << std::endl;
}
void Function2()
{
Timer timer("Function2");
for (int i = 0 ; i < 1000; i++)
std::cout << "Hello World! #" << sqrt(i) << std::endl;
}
int main()
{
Function1();
Function2();
std::cin.get();
}
复用了之前基准测试的Timer类
成功计算了都用多少时间 但是必须在控制台查看 很麻烦
打开chrome浏览器 进入chrome://tracing/网页
#include <iostream>
#include <string>
#include <chrono>
#include <algorithm>
#include <fstream>
#include <thread>
#include <cmath>
struct ProfileResult
{
std::string Name;
long long Start, End;
};
struct InstrumentationSession
{
std::string Name;
};
class Instrumentor
{
private:
InstrumentationSession* m_CurrentSession;
std::ofstream m_OutputStream;
int m_ProfileCount;
public:
Instrumentor()
:
