现在的位置: 首页 > 综合 > 正文

25、记录时间的几种方法

2011年12月05日 ⁄ 综合 ⁄ 共 457字 ⁄ 字号 评论关闭

方法一:

View Code

#include <time.h>
#include
<stdio.h>
void main( void )
{
time_t ltime;
time( &lt
ime );
printf(
"The time is %s\n", ctime(&ltime ) );
}

方法二:

struct timeval start;
struct timeval end;
float time_use = 0;
gettimeofday(&start,NULL); 
//do some operation
gettimeofday(&end,NULL);
time_use = end.tv_sec - start.tv_sec; //in sec

方法三:

struct tm *local;
time_t t;
time(&t);
local=localtime(&t);
char ctime[30];
memset(ctime, 0, 30);
sprintf(time, "[%d:%d:%d]",local->tm_hour, local->tm_min, local->tm_sec);
cout << time;

抱歉!评论已关闭.