대학 생활/C
[C] 프로그램 실행 시간 측정
opid
2013. 10. 8. 13:45
인터넷에 검색하면 많이 나오는 코드이다.
하지만 컴퓨터가 너무 빠른이유인지 모르겠지만... 0초가 나온다.
#include <stdio.h>
#include <time.h>
int main(void) {
clock_t start, end;
double timechk;
start = clock();
int i;
for(i = 0; i < 20000; i++) {
//실행할 문장들
}
end = clock();
timechk = (double)(end - start) / CLOCKS_PER_SEC;
printf("time : %f\n", timechk);
return 0;
}
#include <stdio.h>
#include <sys/time.h>
int main(void) {
struct timeval start, end;
double timechk;
// 현재시간
gettimeofday(&start, NULL);
int i;
for(i = 0; i < 10000; i++) {}
// 계산
gettimeofday(&end, NULL);
timechk = (double)(end.tv_sec) + (double)(end.tv_usec) / 1000000.0 -
(double)(start.tv_sec) - (double)(start.tv_usec) / 1000000.0;
printf("%f\n", timechk);
return 0;
}