귀하는 로그인되어 있지 않습니다. 이대로 편집하면 귀하의 IP 주소가 편집 기록에 남게 됩니다.스팸 방지 검사입니다. 이것을 입력하지 마세요!==기본 사용법== ===간단한 시간 측정=== <syntaxhighlight lang="c++" line=""> #include <iostream> #include <chrono> #include <thread> int main() { using namespace std::chrono; // 시작 시간 기록 auto start = steady_clock::now(); // 측정할 코드 (예: 1초 대기) std::this_thread::sleep_for(milliseconds(1000)); // 종료 시간 기록 auto end = steady_clock::now(); // 경과 시간 계산 auto duration = end - start; // 다양한 단위로 출력 auto ns = duration_cast<nanoseconds>(duration); auto ms = duration_cast<milliseconds>(duration); auto sec = duration_cast<seconds>(duration); std::cout << "경과 시간: " << ns.count() << "ns, " << ms.count() << "ms, " << sec.count() << "초" << std::endl; return 0; } </syntaxhighlight> ===함수 실행 시간 측정=== <syntaxhighlight lang="c++" line=""> #include <iostream> #include <chrono> #include <vector> #include <algorithm> // 측정할 함수 void heavy_computation() { std::vector<int> vec(1000000); std::iota(vec.begin(), vec.end(), 1); std::sort(vec.begin(), vec.end(), std::greater<int>()); } int main() { using namespace std::chrono; auto start = high_resolution_clock::now(); heavy_computation(); auto end = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(end - start); std::cout << "함수 실행 시간: " << duration.count() << " 마이크로초" << std::endl; return 0; } </syntaxhighlight> ===시간 측정 클래스=== 재사용 가능한 시간 측정 클래스를 만들 수 있다: <syntaxhighlight lang="c++" line=""> #include <iostream> #include <chrono> class Timer { private: std::chrono::steady_clock::time_point start_time; public: Timer() : start_time(std::chrono::steady_clock::now()) {} void reset() { start_time = std::chrono::steady_clock::now(); } double elapsed_seconds() const { auto end_time = std::chrono::steady_clock::now(); auto duration = end_time - start_time; return std::chrono::duration<double>(duration).count(); } long long elapsed_milliseconds() const { auto end_time = std::chrono::steady_clock::now(); return std::chrono::duration_cast<std::chrono::milliseconds>( end_time - start_time).count(); } }; int main() { Timer timer; // 어떤 작업 수행 std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::cout << "경과 시간: " << timer.elapsed_seconds() << "초" << std::endl; std::cout << "경과 시간: " << timer.elapsed_milliseconds() << "ms" << std::endl; return 0; } </syntaxhighlight> 편집 요약 가온 위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는 가온 위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요. 또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다. 저작권이 있는 내용을 허가 없이 저장하지 마세요! 취소 편집 도움말 (새 창에서 열림)