인터넷에 검색하면 많이 나오는 코드이다.
하지만 컴퓨터가 너무 빠른이유인지 모르겠지만... 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;
}
'대학 생활 > C' 카테고리의 다른 글
| [C] 성적표 출력하기(파일읽기 scanf함수) (0) | 2013.10.16 |
|---|---|
| [C] Binary Search, Sequential Search(이진찾기, 순차적찾기) 단어검색 (0) | 2013.10.14 |
| [C] strtok() 문자열 구분자로 나누기 (0) | 2013.10.07 |
| [C] strcasecmp() 대소문자 구분안하고 문자열 비교함수 (0) | 2013.10.03 |