[c++] get interval time

특정 구간(들) 을 측정하고 싶을때 사용할 수 있도록 구현 하였다.

IntervalTime.cpp
IntervalTime.h

method
template<typename T>  
void StartClock(T param);

template<typename T>  
double EndClock(T param, bool print = true)throw(std::exception);  

param 으론 char,int,double,string 타입이 들어갈 수 있다.

구간에 이름을 붙여서 측정하는 것이 가능하다.

example
#include"IntervalTime.h"
#include<cstdio>    //intptr_t
#include<cstdlib>    //EXIT_SUCCESS
#ifdef __GNUC__
#include<unistd.h>        //usleep(nano), sleep(sec)
//usleep
void msleep(__useconds_t s) {  
    usleep(s * 1000);
}
//sleep
#elif defined(_MSC_VER)
#include<Windows.h>        //Sleep(micro)
void(_stdcall *msleep)(DWORD s) = Sleep;  
void sleep(DWORD s) {  
    Sleep(s * 1000);
}
#endif 
void foo() {    //foo 함수에 대한 시간 측정 출력  
    times::IntervalTime::StartClock(__FUNCTION__);  
    sleep(1);
    times::IntervalTime::EndClock(__FUNCTION__);
}
int main() {  
    double total = 0.0;
    for (intptr_t i = 0; i < 5; i++) {
        //Whole for 에 대한 시간 측정 시작
        times::IntervalTime::StartClock("Whole for");   
        {
            // 1에 대한 시간 측정 시작
            times::IntervalTime::StartClock(1);     
            {
                sleep(1);
            }
            times::IntervalTime::StartClock(2);
            foo();
            // 1 에 대한 시간 경과 시간 출력
            times::IntervalTime::EndClock(1);           
            {
                sleep(2);
            }
            times::IntervalTime::EndClock(2);
        }
        //Whole for에 대한 경과 시간 출력 및 반환.
        total += times::IntervalTime::EndClock("Whole for");    
    }

    std::cout << "total time : " << total << std::endl;
    return EXIT_SUCCESS;
}
result
                        foo-----------------[curr interval time],[accumulated time],[average time]
                                             1                    1                  1

        1-------------------[curr interval time],[accumulated time],[average time]
                             2.013                2.013              2.013

        2-------------------[curr interval time],[accumulated time],[average time]
                             3.022                3.022              3.022

Whole for-----------[curr interval time],[accumulated time],[average time]  
                     4.033                4.033              4.033

                        foo-----------------[curr interval time],[accumulated time],[average time]
                                             1.001                2.001              1.0005

        1-------------------[curr interval time],[accumulated time],[average time]
                             2.016                4.029              2.0145

        2-------------------[curr interval time],[accumulated time],[average time]
                             3.028                6.05               3.025

Whole for-----------[curr interval time],[accumulated time],[average time]  
                     4.044                8.077              4.0385

                        foo-----------------[curr interval time],[accumulated time],[average time]
                                             1.001                3.002              1.00067

        1-------------------[curr interval time],[accumulated time],[average time]
                             2.026                6.055              2.01833

        2-------------------[curr interval time],[accumulated time],[average time]
                             3.037                9.087              3.029

Whole for-----------[curr interval time],[accumulated time],[average time]  
                     4.053                12.13              4.04333

                        foo-----------------[curr interval time],[accumulated time],[average time]
                                             1.001                4.003              1.00075

        1-------------------[curr interval time],[accumulated time],[average time]
                             2.014                8.069              2.01725

        2-------------------[curr interval time],[accumulated time],[average time]
                             3.025                12.112             3.028

Whole for-----------[curr interval time],[accumulated time],[average time]  
                     4.041                16.171             4.04275

                        foo-----------------[curr interval time],[accumulated time],[average time]
                                             1                    5.003              1.0006

        1-------------------[curr interval time],[accumulated time],[average time]
                             2.018                10.087             2.0174

        2-------------------[curr interval time],[accumulated time],[average time]
                             3.032                15.144             3.0288

Whole for-----------[curr interval time],[accumulated time],[average time]  
                     4.048                20.219             4.0438

total time : 20.219  
계속하려면 아무 키나 누르십시오 . . .