• 【Cherno的C++视频】Visual benchmarking in C++


    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    // how to measure performance visually.
    // chrome://tracing-> it loads in a json file and you'll see.
    
    struct ProfileResult
    {
    	std::string Name;
    	long long Start, End;
    	uint32_t ThreadID;
    };
    
    struct InstrumentationSession
    {
    	std::string Name;
    };
    
    // Formatting a json file and write it out into a file.
    class Instrumentor
    {
    private:
    	InstrumentationSession* m_CurrentSession;
    	std::ofstream m_OutputStream;
    	int m_ProfileCount;
    public:
    	Instrumentor()
    		: m_CurrentSession(nullptr), m_ProfileCount(0)
    	{
    	}
    
    	void BeginSession(const std::string& name, const std::string& filepath = "testFile/VisualBenchmarkingResults.json")
    	{
    		m_OutputStream.open(filepath);
    		WriteHeader();
    		m_CurrentSession = new InstrumentationSession{ name };
    	}
    
    	void EndSession(void)
    	{
    		WriteFooter();
    		m_OutputStream.close();
    		delete m_CurrentSession;
    		m_CurrentSession = nullptr;
    		m_ProfileCount = 0;
    	}
    	// it's the meat of this entire class.
    	void WriteProfile(const ProfileResult& result)
    	{
    		if (m_ProfileCount++ > 0)
    		{
    			m_OutputStream << ",";
    		}
    
    		std::string name = result.Name;
    		std::replace(name.begin(), name.end(), '"', '\'');
    
    		m_OutputStream << "{";
    		m_OutputStream << "\"cat\":\"function\",";
    		m_OutputStream << "\"duration\":" << (result.End - result.Start) << ',';
    		m_OutputStream << "\"name\":\"" << name << "\",";
    		m_OutputStream << "\"ph\":\"X\",";
    		m_OutputStream << "\"pid\":0,";
    		m_OutputStream << "\"tid\":" << result.ThreadID << ",";
    		m_OutputStream << "\"ts\":" << result.Start;
    		m_OutputStream << "}";
    		m_OutputStream.flush();
    	}
    
    	void WriteHeader(void)
    	{
    		m_OutputStream << "{\"otherData\": {},\"traceEvents\":[";
    		m_OutputStream.flush();
    	}
    
    	void WriteFooter(void)
    	{
    		m_OutputStream << "]}";
    		m_OutputStream.flush();
    	}
    
    	static Instrumentor& Get(void)
    	{
    		static Instrumentor* instance = new Instrumentor();
    		return *instance;
    	}
    };
    
    class InstrumentationTimer
    {
    public:
    	InstrumentationTimer(const char* name)
    		: m_Name(name), m_Stopped(false)
    	{
    		m_StartTimepoint = std::chrono::high_resolution_clock::now();
    	}
    
    	~InstrumentationTimer(void)
    	{
    		if (!m_Stopped)
    		{
    			Stop();
    		}
    	}
    	
    	void Stop(void)
    	{
    		auto endTimepoint = std::chrono::high_resolution_clock::now();
    		long long 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();
    		//std::cout << m_Name << ": " << (end - start) << " us\n";
    		uint32_t threadID = std::hash<std::thread::id>{} (std::this_thread::get_id());
    		Instrumentor::Get().WriteProfile({m_Name, start, end, threadID});
    		m_Stopped = true;
    	}
    private:
    	const char* m_Name;
    	std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
    	bool m_Stopped;
    };
    
    #define PROFILING 1
    #if PROFILING
    #define PROFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
    #define PROFILE_FUNCTION() PROFILE_SCOPE(__FUNCSIG__)
    #else
    #define PROFILE_SCOPE(name)
    #endif
    
    namespace Benchmark {
    	void PrintFunction(int value)
    	{
    		PROFILE_FUNCTION();
    		for (int i = 0; i < 1000; i++)
    		{
    			std::cout << "Hello World #" << i + value << std::endl;
    		}
    	}
    
    	void PrintFunction(void)
    	{
    		PROFILE_FUNCTION();
    		for (int i = 0; i < 1000; i++)
    		{
    			std::cout << "Hello World #" << sqrt(i) << std::endl;
    		}
    	}
    
    	void RunBenchmarks(void)
    	{
    		PROFILE_FUNCTION();
    		std::cout << "Running Benchmarks...\n";
    		PrintFunction(2);
    		std::thread a([]() {PrintFunction(); });
    
    		a.join();
    	}
    }
    
    int main(void)
    {
    	Instrumentor::Get().BeginSession("Profile");
    	Benchmark::RunBenchmarks();
    	Instrumentor::Get().EndSession();
    
    	std::cin.get();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172

    VisualBenchmarkingInCpp

  • 相关阅读:
    ijkplayer基于rtsp直播延时的深度优化
    kubernetes--ingress
    从猿六年---C++笔试\面试的不成熟小建议来啦
    【C++】类和对象(下)
    Spring Boot 的创建和运行
    Spring读书笔记——bean创建(上)
    ZZNUOJ_用C语言编写程序实现1370:判断素数(附完整源码)
    云端部署AI换脸开源工具FaceFusion【超详细教程】
    Linux - 输入输出
    Shiro和Spring Security对比
  • 原文地址:https://blog.csdn.net/AlexiaDong/article/details/126372827