블로그 이미지
Kanais
Researcher & Developer 퍼즐을 완성하려면 퍼즐 조각들을 하나 둘씩 맞춰나가야 한다. 인생의 퍼즐 조각들을 하나 둘씩 맞춰나가다 보면 인생이란 퍼즐도 완성되는 날이 오려나...?

calendar

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

Notice

05-02 02:59

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 5. 8. 17:45 Programming/C/C++

개발환경

사용툴       : Visual Studio 2013

프로젝트    : Visual C++ console application

개발날짜    : 2015-05-08



출처 : cplusplus - Any possible way to create a timer to C++ program?

 

Timer 에 대해 찾아보다.. 괜찮은 클래스가 있어 구현해봤다.


start 함수로 Timer 를 시작하여 stop 함수전까지 시간을 측정한다.

그리고 getTime 함수로 결과 시간을 얻을 수 있다.



MyTimer.h

#include <time.h>

 

class MyTimer

{

public:

        MyTimer();

        ~MyTimer();

 

        void           start();

        void           stop();

        void           reset();

        bool           isRunning();

        unsigned long  getTime();

        bool           isOver(unsigned long seconds);

private:

        bool           resetted;

        bool           running;

        unsigned long  beg;

        unsigned long  end;

};


MyTimer.cpp

#include "MyTimer.h" 

 

MyTimer::MyTimer()

{

        resetted = true;

        running = false;

        beg = 0;

        end = 0;

}

  

MyTimer::~MyTimer()

{

}

 

void MyTimer::start() {

        if (!running) {

               if (resetted)

                       beg = (unsigned long)clock();

               else

                       beg -= end - (unsigned long)clock();

               running = true;

               resetted = false;

        }

}

  

void MyTimer::stop() {

        if (running) {

               end = (unsigned long)clock();

               running = false;

        }

}

  

void MyTimer::reset() {

        bool wereRunning = running;

        if (wereRunning)

               stop();

        resetted = true;

        beg = 0;

        end = 0;

        if (wereRunning)

               start();

}

  

bool MyTimer::isRunning() {

        return running;

}

  

unsigned long MyTimer::getTime() {

        if (running)

               return ((unsigned long)clock() - beg) / CLOCKS_PER_SEC;

        else

               return end - beg;

}

  

bool MyTimer::isOver(unsigned long seconds) {

        return seconds >= getTime();

} 


app.cpp

void main() {

        bool quit = false;

        char choice;

        MyTimer t;

        while (!quit) {

               cout << " s   start/stop " << endl;

               cout << " r   reset" << endl;

               cout << " v   view time" << endl;

               cout << " q   quit" << endl;

               cout << endl;

               choice = getch();

               switch (choice) {

               case 's':

                       if (t.isRunning()) {

                              t.stop();

                              cout << "stopped" << endl;

                       }

                       else {

                              t.start();

                              cout << "started" << endl;

                       }

                       break;

               case 'r':

                       t.reset();

                       cout << "resetted" << endl;

                       break;

               case 'v':

                       cout << "time = " << t.getTime() << " ms" << endl;

                       break;

               case 'q':

                       quit = true;

                       break;

               }

               cout << "------------------------------" << endl;

        }

}  







posted by Kanais