1. struct timeval 사용
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
void getElapsedTime(struct timeval Tstart, struct timeval Tend) {
    Tend.tv_usec = Tend.tv_usec - Tstart.tv_usec;
    Tend.tv_sec = Tend.tv_sec - Tstart.tv_sec;
    printf("timeval sub sec : %ld, usec : %ld \n",Tend.tv_sec, Tend.tv_usec);
    Tend.tv_usec += (Tend.tv_sec*1000000);
    printf("Elapsed timeval Time: %lf sec\n",Tend.tv_usec / 1000000.0);
}struct timeval, struct timespec 구조체를 사용하기 위해서는 #include<sys/time.h>의 헤더가 필요하다.
2. struct timespec 사용
#include <stdio.h>
#include <unistd.h>
#include <time.h>
void getElapsedTime(struct timeval Tstart, struct timeval Tend) {
    Tend.tv_usec = Tend.tv_usec - Tstart.tv_usec;
    Tend.tv_sec = Tend.tv_sec - Tstart.tv_sec;
    printf("timeval sub sec : %ld, usec : %ld \n",Tend.tv_sec, Tend.tv_usec);
    Tend.tv_usec += (Tend.tv_sec*1000000);
    printf("Elapsed timeval Time: %lf sec\n",Tend.tv_usec / 1000000.0);
}
void getElapsedTimeSpec(struct timespec Tstart, struct timespec Tend) {
    Tend.tv_nsec = Tend.tv_nsec - Tstart.tv_nsec;
    Tend.tv_sec = Tend.tv_sec - Tstart.tv_sec;
    printf("timespe sub sec : %ld, nsec : %ld \n",Tend.tv_sec, Tend.tv_nsec);
    Tend.tv_nsec += (Tend.tv_sec*1000000000);
    printf("Elapsed timespec Time: %lf sec\n",Tend.tv_nsec / 1000000000.0);
}clock_gettime()함수의 입력 파라미터 "CLOCK_MONOTONIC"을 사용하기 위해서는 #include<time.h> 선인이 필요하다.
clock_gettime()함수의 아규먼트 종류는 아래와 같다.
| 아규먼트 종류 | 설명 | 
| CLOCK_REALTIME | 설정 가능한 시스템 전역인 실제 시간. | 
| CLOCK_REATIME_COARSE | CLOCK_REALTIME 보다 빠르지만 정확하지 않다. | 
| CLOCK_MONOTONIC | 특정 시점(부팅 부터 시작)부터 흐른 시간을 나타낸다. 이 clock은 set 할 수 없다. | 
| CLOCK_MONOTONIC_COARSE | CLOCK_MONOTONIC과 비슷하지만 더 빠르고 정확하지 않다. | 
| CLOCK_MONOTONIC_RAW | CLOCK_MONOTONIC과 비슷하지만 NTP 조정이나 adjtime()에 대한 조정에 영향을 받지 않는 하드웨어 기반 시간에 대한 엑세스를 제공한다. 시스템 절전 대기 시간은 포함하지 않는다. | 
| CLOCK_BOOTTIME | CLOCK_MONOTONIC_RAW와 비슷하지만 시스템의 절전 대기 시간도 포함하는 시간이다. | 
| CLOCK_PROCESS_CPUTIME_ID | 프로세스별 CPU 시간 클럭. 프로세스냄 모든 쓰레드들이 소모한 CPU 시간을 나타낸다. | 
| CLOCK_THREAD_CPUTIME_ID | 특정 쓰레드 환경 CPU 사용 시간을 나타낸다. | 
3. TEST 코드
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
void getElapsedTime(struct timeval Tstart, struct timeval Tend) {
    Tend.tv_usec = Tend.tv_usec - Tstart.tv_usec;
    Tend.tv_sec = Tend.tv_sec - Tstart.tv_sec;
    printf("timeval sub sec : %ld, usec : %ld \n",Tend.tv_sec, Tend.tv_usec);
    Tend.tv_usec += (Tend.tv_sec*1000000);
    printf("Elapsed timeval Time: %lf sec\n",Tend.tv_usec / 1000000.0);
}
void getElapsedTimeSpec(struct timespec Tstart, struct timespec Tend) {
    Tend.tv_nsec = Tend.tv_nsec - Tstart.tv_nsec;
    Tend.tv_sec = Tend.tv_sec - Tstart.tv_sec;
    printf("timespe sub sec : %ld, nsec : %ld \n",Tend.tv_sec, Tend.tv_nsec);
    Tend.tv_nsec += (Tend.tv_sec*1000000000);
    printf("Elapsed timespec Time: %lf sec\n",Tend.tv_nsec / 1000000000.0);
}
void delayfor(void) {
    int i = 0;
    int cnt = 0;
    for(i=0; i<100000000; i++)
        cnt++;
}
int main(void) {
    int i = 0;
    int cnt = 0;
    struct timeval Tstart, Tend;
    struct timespec TSstart, TSend;
    gettimeofday(&Tstart, NULL);
    usleep(1000);
    //delayfor();
    gettimeofday(&Tend,NULL);
    getElapsedTime(Tstart, Tend);
    clock_gettime(CLOCK_MONOTONIC,&TSstart);
    usleep(1000); 
    //delayfor();
    clock_gettime(CLOCK_MONOTONIC,&TSend);
    
    getElapsedTimeSpec(TSstart, TSend);
    return 0;
}
간단한 테스트 프로그램으로 struct timeval와 struct timespec을 이용하여 정밀도를 측정해 보았다.
usleep을 이용하여 약 1msec sleep 구간을 만들어 timeval와 timespec 구조체를 이용하여 시간을 측정하여 보았다.
결과는 아래 로그와 같이 timespec이 조금더 정밀한 것 같다.
$ ./test.out
timeval sub sec : 0, usec : 1126
Elapsed timeval Time: 0.001126 sec
timespe sub sec : 0, nsec : 1147035
Elapsed timespec Time: 0.001147 sec
$ ./test.out
timeval sub sec : 0, usec : 1128
Elapsed timeval Time: 0.001128 sec
timespe sub sec : 0, nsec : 1120208
Elapsed timespec Time: 0.001120 sec
$ ./test.out
timeval sub sec : 0, usec : 1106
Elapsed timeval Time: 0.001106 sec
timespe sub sec : 0, nsec : 1119420
Elapsed timespec Time: 0.001119 sec
$ ./test.out
timeval sub sec : 0, usec : 1130
Elapsed timeval Time: 0.001130 sec
timespe sub sec : 0, nsec : 1120662
Elapsed timespec Time: 0.001121 sec
$ ./test.out
timeval sub sec : 0, usec : 1136
Elapsed timeval Time: 0.001136 sec
timespe sub sec : 0, nsec : 1110551
Elapsed timespec Time: 0.001111 sec
$ ./test.out
timeval sub sec : 0, usec : 1291
Elapsed timeval Time: 0.001291 sec
timespe sub sec : 0, nsec : 1076578
Elapsed timespec Time: 0.001077 sec
$ ./test.out
timeval sub sec : 0, usec : 1374
Elapsed timeval Time: 0.001374 sec
timespe sub sec : 0, nsec : 1082080
Elapsed timespec Time: 0.001082 sec
$ ./test.out
timeval sub sec : 0, usec : 1382
Elapsed timeval Time: 0.001382 sec
timespe sub sec : 0, nsec : 1121709
Elapsed timespec Time: 0.001122 sec
$ ./test.out
timeval sub sec : 0, usec : 1138
Elapsed timeval Time: 0.001138 sec
timespe sub sec : 0, nsec : 1124684
Elapsed timespec Time: 0.001125 sec
$ ./test.out
timeval sub sec : 0, usec : 1121
Elapsed timeval Time: 0.001121 sec
timespe sub sec : 0, nsec : 1125626
Elapsed timespec Time: 0.001126 sec
댓글