반응형

방법1)

두개의 시간 차이를 구하기


time.h 파일에  struct timeval 는 아래와 같이 되어 있다.

---------------------------------------------------------
struct timeval {
        time_t          tv_sec;         /* seconds */
        suseconds_t     tv_usec;        /* and microseconds */
};
---------------------------------------------------------


#include <time.h>

int main(int argc, char *argv[])
{

    struct timeval timeStart;
    struct timeval timeEnd;
    struct timeval timeDiff;

    /* 시작시점 기록 */
    gettimeofday( &timeStart, NULL );

    printf("STARRT_TIME sec[%ld] usec[%ld]\n", timeStart.tv_sec, timeStart.tv_usec);

    /* 실제 처리 작업 수행 */
    /* function()          */

    /* 종료시점 기록 */
    gettimeofday( &timeEnd, NULL );

    printf("END____TIME sec[%ld] usec[%ld]\n", timeEnd.tv_sec, timeEnd.tv_usec);

    /* 처리 시간 계산 */
    timeDiff.tv_sec  = timeEnd.tv_sec  - timeStart.tv_sec;
    timeDiff.tv_usec = timeEnd.tv_usec - timeStart.tv_usec;
    if( timeDiff.tv_usec < 0 )
    {
        timeDiff.tv_sec  = timeDiff.tv_sec  - 1;
        timeDiff.tv_usec = timeDiff.tv_usec + 1000000;
    }

    printf("DIFF___TIME sec[%ld] usec[%ld]\n", timeDiff.tv_sec, timeDiff.tv_usec);

}


처리 결과
###############################################
STARRT_TIME sec[1299839608] usec[359494]
END____TIME sec[1299839608] usec[359964]
DIFF___TIME sec[0] usec[470]
###############################################

방법2)두개의 시간 차이를 구하기
-------------------------------------------------------------------------------------------

#include<time.h>

int main()
{

        time_t start,end;

        double dif;

        time (&start);

        sleep(10);

        time (&end);

        dif = difftime (end,start);

        printf ("%lf\n", dif );

        return 0;
}
--------------------------------------------------------------------------------------------
Standard C Library Functions                         difftime(3C)

NAME
     difftime - computes  the  difference  between  two  calendar times

SYNOPSIS
     #include <time.h>

     double difftime(time_t time1, time_t time0);

DESCRIPTION
     The difftime() function computes the difference between  two calendar times.

RETURN VALUES
     The difftime()  functions  returns  the  difference  (time1 - time0) expressed in seconds as a double.

USAGE
     The difftime() function is provided  because  there  are  no general arithmetic properties defined for type time_t.

ATTRIBUTES
     See attributes(5) for descriptions of the  following  attributes:

     ____________________________________________________________
    |       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |
    |_____________________________|_____________________________|
    | Interface Stability         | Standard                    |
    |_____________________________|_____________________________|
    | MT-Level                    | MT-Safe                     |
    |_____________________________|_____________________________|

SEE ALSO
     ctime(3C), attributes(5), standards(5)

--------------------------------------------------------------------------------------------

반응형
Posted by 공간사랑
,