My Quick Notes

Ain't Nobody Got Time to Read

October 13, 2021

Tính thời gian thực thi thực sự trong C++

#include <chrono>

int main() {
	std::chrono::time_point <std::chrono::system_clock, std::chrono::duration<double, std::milli>> then = std::chrono::high_resolution_clock::now();;

	// ... do something ...

	std::chrono::time_point <std::chrono::system_clock, std::chrono::duration<double, std::milli>> now = std::chrono::high_resolution_clock::now();;
	double elapseTimeInMS = (now - then).count();

	return 0;
}

Cách viết khác ngắn hơn:

#include <chrono>

using namespace std;

int main() {
	auto then = chrono::high_resolution_clock::now();

	// ... do something ...

	auto now = chrono::high_resolution_clock::now();
	double elapseTimeInMS = chrono::duration<double, milli>(now - then).count();

	return 0;
}
Bài trướcCấu hình Raspberry Pi 4 thành Wireless Access Point
Bài tiếp[Linux] Đánh số trang cho file PDF từ terminal

Bình luận