如何获取程序运行时间?

1. CTime 方法(秒级)

CTime t1 = CTime::GetCurrentTime();
Sleep(xxxx);
CTime t2 
= CTime::GetCurrentTime();

CTimeSpan tSp 
= t2 - t1;
DWORD nSec 
= tSp.GetTotalSeconds();

// 取得累加上时、分之后所有的秒
// CTime是MFC类库

2. time 方法(秒级)
time_t t1;
time_t t2;

time(
& t1);
Sleep(xxxx);
time(
& t2);

unsigned tDiff 
=  (unsigned)difftime(t2, t1);

//  time_t就是unsigned型
3. _ftime 方法(毫秒级)
struct  timeb  {
        time_t time;        
// 逝去的秒    
        unsigned short millitm;                     // 不足秒的毫秒部分
        short timezone;                        // 与时区相关?
        short dstflag;                        // 与时区相关?
        }
;

struct  _timeb tb1;
struct  _timeb tb2;

_ftime(tb1);
Sleep(xxxx);
_ftime(tb2);

unsigned nMs 
=  tb2.millitm  -  tb1.millitm  +  (tb2.time  -  tb1.time) * 1000 ;
4.取日期
_strdate( char  buf[]);     //  如:05/03/94
5.取时间
_strtime( char  buf[]);   //  如:21:51:03
6.附录
struct  timeb  {
        time_t time;                      
// 逝去的秒    
        unsigned short millitm;  // 不足秒的毫秒部分
        short timezone;                // 与时区相关?
        short dstflag;                    // 与时区相关?
        }
;

time_t  就是unsigned型

struct  tm  {
    tm_sec,        
// Seconds after minute (0 – 59)
    tm_min,        // Minutes after hour (0 – 59)
    tm_hour,       // Hours since midnight (0 – 23)
    tm_mday,     // Day of month (1 – 31)
    tm_mon,      // Month (0 – 11; January = 0)
    tm_year,       // Year (current year minus 1900)
    tm_wday,     // Day of week (0 – 6; Sunday = 0)
    tm_yday,      // Day of year (0 – 365; January 1 = 0)
    tm_isdst,     // Always 0 for gmtime
}
;

函数  
char *  ctime( const  time_t *  timer);
          把time_t(逝去的秒数)根据时区,转化为相应的时间的字符串
          如:
" Apr 29 12:25:12 1994 "

函数  
struct  tm *  gmtime( const  time_t *  timer);
函数  
struct  tm *  localtime( const  time_t *  timer);
          把time_t(逝去的秒数)转化为“全球时”或“本地时”的tm结构    
    
函数  time_t mktime(
struct  tm  * timeptr);
          把tm结构转化为time_t(逝去的秒数)

函数  
char   * asctime( const   struct  tm  * timeptr);
          把tm结构转化为字符串,如
" Sun May 01 20:27:01 1994 "
    
函数  size_t strftime(
char   * strDest, size_t maxsize,  const   char   * format,  const   struct  tm  * timeptr);
          把tm结构格式化,得到想要的某种格式

函数  _tzset
         设置环境变量

变量  _tzname
          操作全局变量
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值