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