Worker
클래스에서 잠깐 사용하는 Timer
클래스
main.cpp
#include "Timer.h"
#include "Worker.h"
int main()
{
Worker().doSomething();
}
/* stdout
4e-07
*/
Timer.h
#pragma once
#include <iostream>
#include <chrono>
using namespace std;
class Timer
{
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1>>;
std::chrono::time_point<clock_t> start_time = clock_t::now();
public:
void elapsed()
{
std::chrono::time_point<clock_t> end_time = clock_t::now();
cout << std::chrono::duration_cast<second_t>(end_time - start_time).count() << endl;
}
};
Worker.h
#pragma once
class Worker
{
public:
void doSomething();
};
Worker.cpp
#include "Worker.h"
#include "Timer.h"
void Worker::doSomething()
{
Timer timer;
// do some work here
timer.elapsed();
}