250x250
Notice
Recent Posts
«   2024/11   »
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
관리 메뉴

일상 코딩

[C++/10.05] 의존 관계, Dependecy on, Timer, Chrono 본문

C++/따배C++ 10강 객체들 사이의 관계

[C++/10.05] 의존 관계, Dependecy on, Timer, Chrono

polarcompass 2021. 11. 3. 23:01
728x90

1. main.cpp

#include "Worker.h"

// using namespace std;

int main()
{
    Worker().doSomething();
    return 0;
}

2.  Timer.h

#pragma once

#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>

class Timer
{
private:
    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();
        std::cout << std::chrono::duration_cast<second_t>(end_time - start_time).count() << std::endl;
    }
};

3. Worker.h

#pragma once

class Worker
{
public:
    void doSomething();
};

4. Worker.cpp

// #pragma once

#include "Timer.h"
#include "Worker.h"

void Worker::doSomething()
{
    Timer timer; // start timer
    int hap = 0;
    for (int i = 0; i < 10000; i++)
    {
        hap += i;
    }

    timer.elapsed();
}
728x90