티스토리 뷰
목차
Linux 함수 실행 시간 측정 (리눅스 Time 함수 명령어 응용)
리눅스(Linux)에서 시간 측정을 위해 필요한 TIME 함수의 사용 예입니다.
time.h 파일을 인클루드하고, time_t의 객체를 생성하는 것으로 부터 시작합니다.
printf 문을 살펴보면, 초, 분, 시간, 그리고 시스템의 시간 측정 결과까지 출력할 수 있다는 것도 확인이 됩니다. 아래 코드에 적힌 주석을 보시면서 코드를 이해해 보세요.
그렇게 어렵진 않습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> #include <time.h> int main(){ time_t tim; struct tm *t; tim = time(NULL); // linux time 구조체를 구분하여 주는 함수 // localtime 을 이용하여 구분해야 원하는 시간만 따로 뽑아낼 수 있다. t = localtime(&tim); // 원하는 시간을 따로 나타내기 printf("second: %d \n", t->tm_sec); printf("minute: %d \n", t->tm_min); printf("hour: %d \n", t->tm_hour); // 시간 측정, 현재 경과된 초 printf("unix time: %d \n", tim); // 년월일 형식으로 나타내기 // ctime 은 년월일과 요일등을 스트링형으로 반환한다. printf("current time: %s \n", ctime(&tim) ); return 0; } | cs |
리눅스 TIME 함수 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 | struct tm { int tm_sec; -- Seconds -- int tm_min; -- Minutes -- int tm_hour; -- Hour (0--23) -- int tm_mday; -- Day of month (1--31) -- int tm_mon; -- Month (0--11) -- int tm_year; -- Year (calendar year minus 1900) -- int tm_wday; -- Weekday (0--6; Sunday = 0) -- int tm_yday; -- Day of year (0--365) -- int tm_isdst; -- 0 if daylight savings time is not in effect) -- }; | cs |
linux tm 구조체도 살펴보세요.
이 리눅스 구조체를 보시고 Time 함수도 함께 보셔야 원하는 것을 출력하는 데, 도움이 됩니다.
Linux 함수 실행 시간 측정 (리눅스 Time 함수 명령어 응용)