FCT
载入中...
搜索中...
未找到
ScopeTimer.h
浏览该文件的文档.
1//
2// Created by Administrator on 2025/5/11.
3//
4
5#ifndef SCOPETIMER_H
6#define SCOPETIMER_H
7#include "OutStream.h"
8#include "../ThirdParty.h"
9namespace FCT
10{
11 class ScopeTimer {
12 private:
13 std::string name;
14 std::chrono::high_resolution_clock::time_point startTime;
15 bool stopped;
16 static std::map<std::string, double> timings;
17 public:
18 ScopeTimer(const std::string& timerName)
19 : name(timerName), stopped(false) {
20 startTime = std::chrono::high_resolution_clock::now();
21 }
22
24 if (!stopped) {
25 stop();
26 }
27 }
28
29 void stop() {
30 auto endTime = std::chrono::high_resolution_clock::now();
31 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
32
33 timings[name] = duration;
34 stopped = true;
35 }
36
37 static double getTime(const std::string& timerName) {
38 auto it = timings.find(timerName);
39 if (it != timings.end()) {
40 return it->second;
41 }
42 return 0.0;
43 }
44 static void printAll(std::ostream& out = fout) {
45 out << "===== ScopeTimer Results =====\n";
46 for (const auto& timing : timings) {
47 out << timing.first << ": " << timing.second << " us\n";
48 }
49 out << "============================\n";
50 }
51 };
52} // namespace FCT
53#define FCTSCOPETIMER(name) FCT::ScopeTimer name##Timer(#name)
54#endif //SCOPETIMER_H
ScopeTimer(const std::string &timerName)
static void printAll(std::ostream &out=fout)
static std::map< std::string, double > timings
static double getTime(const std::string &timerName)
std::chrono::high_resolution_clock::time_point startTime
std::ostream & fout