math.h-lm 옵션을 추가해야함.#include <math.h>
// 반올림 함수 구현
floor((x*pow(10.0, n)+0.5))/pow(10.0, n);
stdlib.h#include <stdlib.h>
#include <time.h>
// 랜덤 함수 구현
srand((unsigned)time(NULL));
rand()%5; // 0~4
time.h//현재 시간 구하기
time_t now; // 32비트 컴퓨터에서는 최대 UTC 2038/1/18일 까지 표현 가능
// 64비트 컴퓨터에서는 최대 UTC 3000/12/31일 까지 표현 가능
now=time(NULL);
//time_t now2;
//time(&now2); // now2=time(NULL) 과 동일하다.
// time_t 변수를 tm 구조체로 변환
struct tm *pt;
pt=gmtime(&now);
printf("Global Time: %d년 %d월 %d일 %d시 %d분 %d초\n",
pt->tm_year+1900, pt->tm_mon+1, pt->tm_mday,
pt->tm_hour, pt->tm_min, pt->tm_sec
);
//struct tm{
// int tm_sec; /* Seconds. [0-60] (1 leap second) */
// int tm_min; /* Minutes. [0-59] */
// int tm_hour; /* Hours. [0-23] */
// int tm_mday; /* Day. [1-31] */
// int tm_mon; /* Month. [0-11] */
// int tm_year; /* Year - 1900. */
// int tm_wday; /* Day of week. [0-6] */
// int tm_yday; /* Days in year.[0-365] */
// int tm_isdst; /* DST. [-1/0/1] # 일약 절약 시간과의 차*/
//}
// tm 구조체를 바탕으로 시간을 문자열로 표현하기
char Format[128];
strftime(Format, 128, "%Y %B %d %A %I:%M:%S %p", localtime(&now));
printf("Local Time: %s\n", Format);
//mktime함수는 tm구조체를 time_t형으로 바꾼다.
// clock 함수는 프로그램이 실행을 시작한 후의 경과된 시간을 조사한다.
// 이 함수가 조사한 값을 CLOCKS_PER_SEC로 나누면 프로그램 실행 후의 경과초를 알 수 있다.
clock_t t1, t2;
int count=0;
t1=clock();
for(;;){
count++;
t2=clock();
if(t2-t1 > 3*CLOCKS_PER_SEC){ // 대략 3초를 의미함.
printf("%d\n", count);
break;
}
}
// difftime은 두 개의 시간에 대한 차이를 초단위로 반환한다.
// 계산 결과가 초단위 이기 때문에 정밀한 시간 계산이나 코드의 성능 측정 등에 쓰기는 무리다.
time_t start_time, end_time;
start_time=time(NULL);
for(int j;j<500000000;j++){
double s=sin(j*3.1416/180);
}
end_time=time(NULL);
printf("걸린 시간: %f 초 \n", difftime(end_time, start_time));
출처 : 혼자 연구하는 C/C++ 1 / 김상형 저 / 와우북스