실행 시간 측정하기
#ifdef TIMES
start = dclock();
#endif
//시간 측정 부분
#ifdef TIMES
end = dclock() - start();
printf(end);
#endif
$gcc -DTIMES -o t1 t1.c
- -D
- compiler directive 명령어
- TIMES
- 임의로 이름 설정 가능함
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
main() {
int status;
pid_t pid;
#ifdef TIMES
struct timeval stime,etime;
int time_result;
#endif
pid = fork();
#ifdef TIMES
gettimeofday(&stime, NULL);
#endif
if (pid == 0) {
printf("--> Child process\n");
sleep(5);
exit(3);
}
printf("--> Parent process\n");
while (waitpid(pid, &status, WNOHANG) == 0) {
printf("Parent still wait..\n");
sleep(1);
}
printf("Child Exit Status : %d\n", status>>8);
#ifdef TIMES
gettimeofday(&etime, NULL);
time_result = etime.tv_usec - stime.tv_usec;
printf("TIMES == %ld %ld %ld\n", etime.tv_usec, stime.tv_usec, time_result);
#endif
}