HourMeter.h
#pragma once
#include <iostream>
#include <chrono>
class HourMeter
{
public:
HourMeter();
~HourMeter();
void startMeasure();
void endMeasure();
double getLatestDuration();
private:
std::chrono::system_clock::time_point begin;
std::chrono::system_clock::time_point end;
std::chrono::duration<double> result_sec;
bool isMeasuring;
};
HourMeter.cpp
#include "HourMeter.h"
HourMeter::HourMeter() :
isMeasuring(false)
{
printf("START HourMeter\n");
}
HourMeter::~HourMeter()
{
printf("START ~HourMeter\n");
}
void HourMeter::startMeasure()
{
if (isMeasuring == true) {
printf("FAIL startMeasure; already measuring\n");
throw std::runtime_error("startMeasure");
}
isMeasuring = true;
begin = std::chrono::system_clock::now();
}
void HourMeter::endMeasure()
{
if (isMeasuring == false) {
printf("FAIL endMeasure; not measuring\n");
throw std::runtime_error("endMeasure");
}
end = std::chrono::system_clock::now();
result_sec = end - begin;
isMeasuring = false;
printf("수행시간: %lf\n", result_sec);
}
double HourMeter::getLatestDuration()
{
return result_sec.count();
}
main.cpp
#include <iostream>
#include "HourMeter.h"
using namespace std;
void main() {
HourMeter hourMeter;
try {
hourMeter.startMeasure();
for (int i = 0; i < 2048; i++) {
printf("Hello world\n");
}
hourMeter.endMeasure();
}
catch (std::exception & ex) {
return;
}
printf("마지막 시간: %lf\n", hourMeter.getLatestDuration());
}
결과
'컴퓨터 공학 > C++' 카테고리의 다른 글
[C++] 구조체 정렬 크기 조절하기 (0) | 2019.11.14 |
---|---|
vector와 array의 처리 성능 높이는 방법 (실험) (1) | 2019.11.14 |
C++ 함수 내 람다 함수를 통해 코드 정리 (0) | 2019.11.14 |
const_cast 사용 예시 (0) | 2019.11.14 |
C++ exit()와 quick_exit()의 차이점 (0) | 2019.05.24 |