您的位置:首页 > 其它

C 标准库 —— time.h

2016-05-11 18:00 411 查看
C 标准库—— string.h

C 标准库 —— time.h

C 标准库 —— limits.h

C 标准库 —— stdio.h

C 标准库—— stdlib.h(包括 rand srand 的实现)

C 标准库——<cmath>/<math.h>

C 标准库—— assert.h

time_t

typedef __time32_t time_t;
typedef __int32 __time32_t;
#  define __int32 long
// 也即 time_t 其实是 32 位 long 类型


也即
time_t
可取的最大值为:0x7fffffff。

time_t biggest = 0x7fffffff;
printf("biggest = %d\n", biggest);


1. 基本类型定义及 API

(1)time() ⇒ 获取当前时间、

time_t now = time(0);
printf("now is: %s\n", ctime(&now));
// now: Wed May 11 12:55:09 2016


(2)ctime() ⇒ 把参数转换为当地时间

time_t biggest = 0x7fffffff;
printf("biggest: %s", ctime(&biggest));
// biggest: Tue Jan 19 11:14:07 2038


2. 运行时间

使用 clock() 函数:返回从程序运行时刻开始的时钟周期数,返回值为 (typedef long clock_t);

宏 CLOCKS_PER_SEC:每秒钟包含多少个时钟单元数(#define CLOCKS_PER_SEC ((clock_t)1000)

clock_t start_time = clock();
// 待测代码段
cout << "elpsed time(ms): " << static_cast<double>(clock() - start_time)/CLOCKS_PER_SEC * 1000 << endl;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: