Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- package
- go언어
- write
- Linux
- go
- bitcoin
- Python
- 책
- windows
- c++
- Sync
- 리뷰
- 영화
- GO 언어
- FOR
- tcp
- Callback
- File
- JavaScript
- API
- http
- window
- install
- C
- Close
- Golang
- mutex
- channel
- json
- range
Archives
- Today
- Total
Code Habit
[c++] chrono 라이브러리 본문
std::chrono 라이버리는 c++ 11부터 제공되는 라이브러리로 시간을 측정하고 관리하는 다양한 유형의 클래스와 함수를 제공하여 시간 간격, 지연 및 시간 단위 변환 등 시간을 관련 작업을 수행한다.
1. 시간 간격 구하기
#include <iostream>
#include <chrono>
int main() {
// get start_time
auto start_time = std::chrono::high_resolution_clock::now();
// wait 2 second
std::this_thread::sleep_for(std::chrono::seconds(2));
// get end time
auto end_time = std::chrono::high_resolution_clock::now();
// cal duration
std::chrono::duration<double> elapsed_time = end_time - start_time;
// print
std::cout << elapsed_time.count() << "초" << std::endl;
return 0;
}
2. 일정 시간까지 대기
#include <iostream>
#include <chrono>
#include <thread>
int main() {
// get now
auto start_time = std::chrono::steady_clock::now();
// get time after 5 seconds
auto end_time = start_time + std::Chrono::seconds(5);
// wait until 5 seconds
std::this_thread::sleep_until(end);
return 0;
}
hight_resolution_clock은 시스템에 기반하는 시간으로 성능측정과 같은 정밀한 시간계산이 필요할 때 주로 쓰이고 steady_clock은 시스템에 영향받지 않는 시간으로 시간이 균일하게 흐르는 것을 보장받는다. 타이머나 대기조건에 주로 쓰인다.