|
60鱼币
本帖最后由 御坂19090 于 2023-3-20 13:07 编辑
拖进去。什么反应都没有
开始打开是这样,"C:\\Users\\misaka\\Desktop\\QQ录屏20230319233349.mp4"。运行这个得到的- #include <iostream>
- #include <string>
- #include <chrono>
- #include <algorithm>
- #include <fstream>
- #include <cmath>
- //配置文件结果
- struct ProfileResult
- {
- std::string Name;
- long long Start, End;
- };
- //检测会话
- struct InstrumentationSession
- {
- std::string Name;
- };
- //格式化一个json文件,并将其写入一个文件
- class Instrumentor
- {
- private:
- //当前会话
- InstrumentationSession* m_CurrentSession;
- //输出流
- std::ofstream m_OutputStream;
- //配置文件计数
- int m_ProfileCount;
- public:
- Instrumentor()
- : m_CurrentSession(nullptr), m_ProfileCount(0)
- {}
- //创建一个有给定文件名的新文件;默认results.json
- void BeginSession(const std::string& name, const std::string filepath = "results.json")
- {
- //打开一个文件
- m_OutputStream.open(filepath);
- WriteHeader();
- m_CurrentSession = new InstrumentationSession{ name };
- }
- //写了一个简单的页脚,关闭文件等等事情
- void EndSession()
- {
- WriteFooter();
- m_OutputStream.close();
- delete m_CurrentSession;
- m_CurrentSession = nullptr;
- m_ProfileCount = 0;
- }
- //写时间分析数据
- 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 << ""dur":" << (result.End - result.Start) << ',';
- m_OutputStream << ""name":"" << name << "",";
- m_OutputStream << ""ph":"X",";
- m_OutputStream << ""pid":0,";
- m_OutputStream << ""tid":0,";
- m_OutputStream << ""ts":" << result.Start;
- m_OutputStream << "}";
- //这个末尾,每输出完一部分json进入输出流,就进行刷新,把这些数据流到文件中
- //要这样做的原因是,在程序崩溃或终止,不会丢失所有提供的数据
- m_OutputStream.flush();
- }
- //写一个文件头,是json文件的开头,是Chrome tracing需要的一个特定的格式
- void WriteHeader()
- {
- m_OutputStream << "{"otherData": {},"traxeEvents":[";
- m_OutputStream.flush();
- }
- //写一个简单的页脚
- void WriteFooter()
- {
- m_OutputStream << "]}";
- m_OutputStream.flush();
- }
- //返回一个Instrumentor类。(静态)
- static Instrumentor& Get()
- {
- 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()
- {
- if (!m_Stopped)
- Stop();
- }
- void Stop()
- {
- 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();
- long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
- Instrumentor::Get().WriteProfile({m_Name, start, end});
-
- m_Stopped = true;
- }
- private:
- const char* m_Name;
- std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
- bool m_Stopped;
- };
- #define RPOFLING 1
- #if RPOFLING
- //##__LINE__;为变量取一个唯一的名字,以防我们有很多这样的东西
- #define RPOFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
- //调用PROFILESCOPE宏,但对于name,它会接受函数的名学,我们可以用这个编译宏FUNCTION二来做
- #define RPOFILE_FUNCTION() RPOFILE_SCOPE(__FUNCTION__)
- #else
- #define RPOFLING_SCOPE(name)
- #endif
- void Function1()
- {
- RPOFILE_FUNCTION();
- for (int i = 0; i < 1000; i ++)
- std::cout << "Hello Word #" << i << std::endl;
- }
- void Function2()
- {
- RPOFILE_FUNCTION();
- for (int i = 0; i < 1000; i++)
- std::cout << "Hello Word #" << sqrt(i) << std::endl;
- }
- void RunBenchmarks()
- {
- RPOFILE_FUNCTION();
- std::cout << "Running Benchmarks\n";
- Function1();
- Function2();
- }
- void main()
- {
- Instrumentor::Get().BeginSession("Profile");
- RunBenchmarks();
- Instrumentor::Get().EndSession();
- std::cin.get();
- }
- }
复制代码
json文件内容
- {"otherData": {},"traxeEvents":[{"cat": "function","dur":98540,"name":"Function1","ph":"X","pid":0,"tid":0,"ts":268614872780},{"cat": "function","dur":171829,"name":"Function2","ph":"X","pid":0,"tid":0,"ts":268614971414},{"cat": "function","dur":270628,"name":"RunBenchmarks","ph":"X","pid":0,"tid":0,"ts":268614872687}]}
复制代码
因为你的第 83 行写错了,你把 trace 写成了 traxe
- #include <iostream>
- #include <string>
- #include <chrono>
- #include <algorithm>
- #include <fstream>
- #include <cmath>
- //配置文件结果
- struct ProfileResult
- {
- std::string Name;
- long long Start, End;
- };
- //检测会话
- struct InstrumentationSession
- {
- std::string Name;
- };
- //格式化一个json文件,并将其写入一个文件
- class Instrumentor
- {
- private:
- //当前会话
- InstrumentationSession* m_CurrentSession;
- //输出流
- std::ofstream m_OutputStream;
- //配置文件计数
- int m_ProfileCount;
- public:
- Instrumentor()
- : m_CurrentSession(nullptr), m_ProfileCount(0)
- {}
- //创建一个有给定文件名的新文件;默认results.json
- void BeginSession(const std::string& name, const std::string filepath = "results.json")
- {
- //打开一个文件
- m_OutputStream.open(filepath);
- WriteHeader();
- m_CurrentSession = new InstrumentationSession{ name };
- }
- //写了一个简单的页脚,关闭文件等等事情
- void EndSession()
- {
- WriteFooter();
- m_OutputStream.close();
- delete m_CurrentSession;
- m_CurrentSession = nullptr;
- m_ProfileCount = 0;
- }
- //写时间分析数据
- 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 << ""dur":" << (result.End - result.Start) << ',';
- m_OutputStream << ""name":"" << name << "",";
- m_OutputStream << ""ph":"X",";
- m_OutputStream << ""pid":0,";
- m_OutputStream << ""tid":0,";
- m_OutputStream << ""ts":" << result.Start;
- m_OutputStream << "}";
- //这个末尾,每输出完一部分json进入输出流,就进行刷新,把这些数据流到文件中
- //要这样做的原因是,在程序崩溃或终止,不会丢失所有提供的数据
- m_OutputStream.flush();
- }
- //写一个文件头,是json文件的开头,是Chrome tracing需要的一个特定的格式
- void WriteHeader()
- {
- m_OutputStream << "{"otherData": {},"traceEvents":["; // 这一行写错了,trace 写成了 traxe
- m_OutputStream.flush();
- }
- //写一个简单的页脚
- void WriteFooter()
- {
- m_OutputStream << "]}";
- m_OutputStream.flush();
- }
- //返回一个Instrumentor类。(静态)
- static Instrumentor& Get()
- {
- 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()
- {
- if (!m_Stopped)
- Stop();
- }
- void Stop()
- {
- 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();
- long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
- Instrumentor::Get().WriteProfile({m_Name, start, end});
-
- m_Stopped = true;
- }
- private:
- const char* m_Name;
- std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
- bool m_Stopped;
- };
- #define RPOFLING 1
- #if RPOFLING
- //##__LINE__;为变量取一个唯一的名字,以防我们有很多这样的东西
- #define RPOFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
- //调用PROFILESCOPE宏,但对于name,它会接受函数的名学,我们可以用这个编译宏FUNCTION二来做
- #define RPOFILE_FUNCTION() RPOFILE_SCOPE(__FUNCTION__)
- #else
- #define RPOFLING_SCOPE(name)
- #endif
- void Function1()
- {
- RPOFILE_FUNCTION();
- for (int i = 0; i < 1000; i ++)
- std::cout << "Hello Word #" << i << std::endl;
- }
- void Function2()
- {
- RPOFILE_FUNCTION();
- for (int i = 0; i < 1000; i++)
- std::cout << "Hello Word #" << sqrt(i) << std::endl;
- }
- void RunBenchmarks()
- {
- RPOFILE_FUNCTION();
- std::cout << "Running Benchmarks\n";
- Function1();
- Function2();
- }
- void main()
- {
- Instrumentor::Get().BeginSession("Profile");
- RunBenchmarks();
- Instrumentor::Get().EndSession();
- std::cin.get();
- }
- }
复制代码
|
最佳答案
查看完整内容
因为你的第 83 行写错了,你把 trace 写成了 traxe
|