C++时间函数简单记录

1,计时函数

clock_t clock ( void );

返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的时钟计时单元(clock tick)数。
头文件:<time.h>或者<ctime>

#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif

将clock_t转换成秒的操作
second = clock / CLOCKS_PER_SEC

示例代码:
#include <iostream>
#include <ctime>
#include <map>

int main()
{
	std::map<int, int> col1;
	for (int i = 0; i < 10000; ++i) {
		col1.insert(std::make_pair(i, i));
	}

	clock_t t = clock();

	printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}

执行结果;
It took me 93 clicks (0.093000 seconds).
Press any key to continue

2,日历函数
time_t time ( time_t * timer );

 返回当前的日历时间,1970年1月1日距离现在的秒数
 头文件:<time.h>或者<ctime>

typedef long    time_t;         /* time value */

示例代码:
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t seconds;

  seconds = time (NULL);
  printf ("%ld hours since January 1, 1970", seconds/3600);
  
  return 0;
}

执行结果:
373234 hours since January 1, 1970Press any key to continue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值