solitaryclown

基于析构函数计算代码执行时间

2023-06-15
solitaryclown
C++

#include<iostream>
#include<thread>
#include<chrono>


struct Timer {
	std::chrono::time_point<std::chrono::steady_clock> start, end;
	std::chrono::duration<float> duration;
	Timer() {
		start = std::chrono::high_resolution_clock::now();
	}
	~Timer() {
		end = std::chrono::high_resolution_clock::now();
		duration = end - start;
		float ms = duration.count() * 1000.0f;
		std::cout << "cost time:" << ms << " ms.";
	}
};
void DoWork() {
	Timer timer;
	for (int i = 0; i < 100; i++) {
		std::cout << "working..." << std::endl;
	}
}
int main() {

	std::thread worker(&DoWork);
	
	worker.join();
	std::cin.get();
}
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
working...
cost time:4.2161 ms.

下一篇 opencv-HighGUI

Comments

Content