四个变量类型:
size_t
无符号整数类型,是 sizeof
关键字的结果
clock_t
适合存储处理器时间的类型,返回从程序已走过的时钟计时单元数
time_t is
适合存储日历时间类型
struct tm
用来保存时间和日期的结构
struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59 */
int tm_hour; /* 小时,范围从 0 到 23 */
int tm_mday; /* 一月中的第几天,范围从 1 到 31 */
int tm_mon; /* 月,范围从 0 到 11 */
int tm_year; /* 自 1900 年起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};
两个宏:
NULL
一个空指针常量的值
CLOCKS_PER_SEC
表示CPU时钟一秒钟所走过的时钟单位数
操作日期和时间的函数:
char *asctime(const struct tm *timeptr)
返回一个指向字符串的指针,它代表了结构 timeptr 的日期和时间。
clock_t clock(void)
返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。
char *ctime(const time_t *timer)
返回一个表示当地时间的字符串,当地时间是基于参数 timer。
double difftime(time_t time1, time_t time2)
返回 time1 和 time2 之间相差的秒数 (time1-time2)。
struct tm *gmtime(const time_t *timer)
timer 的值被分解为 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
struct tm *localtime(const time_t *timer)
timer 的值被分解为 tm 结构,并用本地时区表示。
time_t mktime(struct tm *timeptr)
把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。
time_t time(time_t *timer)
计算当前日历时间,并把它编码成 time_t 格式。
常见应用
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
clock_t start,end;
start=clock();
int sum=0;
for(int i=1;i<=1000;i++)
{
for(int j=1;j<=1000;j++)
{
for(int k=1;k<=1000;k++)
{
sum+=1;
}
}
}
end=clock();
cout<<"start: "<<start<<' '<<"end: "<<end<<endl;
cout<<"CLOCKS_PER_SEC is: "<<CLOCKS_PER_SEC<<endl;
printf("程序耗时: %.5lfms\n",(double)(end-start)/CLOCKS_PER_SEC*1000);
}
/*
start: 111 end: 2317
CLOCKS_PER_SEC is: 1000
程序耗时: 2206.00000ms
*/