반응형
C 인 경우 time.h 를 포함하고 C++인 경우 ctime을 포함한다.
#include <time.h>
#include <ctime>
시간 측정은 두 헤더에 모두 포함된 clock()함수를 사용하면 된다.
clock_t clock(void);
반환형은 clock_t 타입인데 따라가보면 그냥 long 타입이다.
typedef long clock_t;
그리고 반환하는 내용으로 프로그램 실행 시작부터 경과된 시간을 ms 단위의 정수형으로 반환한다.
사용방법
#include <stdio.h>
#include <time.h>
int main()
{
long startTime = (long)clock();
while (1)
{
long currentTime = (long)clock();
if (currentTime - startTime >= 1000UL)
{
printf("currentTime = %ld, startTime = %ld\n", currentTime, startTime);
startTime = (long)clock();
// do something...
}
}
return 0;
}

반응형
'프로그래밍 > C' 카테고리의 다른 글
| C ] strtol, strtoul, strtof (0) | 2023.01.15 |
|---|---|
| C ] strtok, strtok_r : 구분 문자로 문자열 쪼개기 (0) | 2023.01.15 |
| C ] 가변인자 매크로를 활용한 디버그 로그 남기기, Predefined macros (0) | 2022.12.11 |
| 전처리기 ] # 문자열화 연산자 (0) | 2022.12.09 |
| C ] 빅엔디안 리틀엔디안 변환함수 구현 + 매크로 함수 작성시 주의점 (0) | 2022.10.22 |