C标准时间库

四个变量类型:

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
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alveus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值