VC/MFC中计算程序/系统运行时间

VC/MFC中计算程序/系统运行时间

SkySeraph Dec.30th 2010  HQU

Email:zgzhaobo@gmail.com    QQ:452728574

Latest Modified Date:Dec.30th 2010 HQU

法一 利用GetTickCount函数 

获取程序运行时间 

 
  
。。。

long t1 = GetTickCount(); // 程序段开始前取得系统运行时间(ms)

。。。。。。
// to do sth

long t2 = GetTickCount(); // 程序段结束后取得系统运行时间(ms)

cout
<< t2 - t1 << endl; // 前后之差即程序运行时间

。。。

 

 

获取系统运行时间

 

代码
 
   
CString str;

// 获取程序运行时间

long t1 = GetTickCount(); // 程序段开始前取得系统运行时间(ms)

// Sleep(500);

// AfxMessageBox("do something...");

。。。。。。
// to do sth

long t2 = GetTickCount(); // 程序段结束后取得系统运行时间(ms)

str.Format(
" time:%dms " ,t2 - t1); // 前后之差即程序运行时间

AfxMessageBox(str);

 

    

法二 利用C/C++计时函数

 获取程序运行时间

 

代码
 
   
#include " time.h "

。。。

clock_t start, finish;

start
= clock();

。。。。。。
// to do sth

finish
= clock();

// cout<<(double)(finish-start)/CLOCKS_PER_SEC<<" seconds"<<endl;

printf(
" %f seconds\n " ,( double )(finish - start) / CLOCKS_PER_SEC);



。。。

 

 

函数/参数说明

clock()

C/C++计时函数,与其相关的数据类型是clock_t

返回:从"此程序进程开启"到"程序中调用clock()函数"之间CPU计时单元数,MSDN中称挂钟时间(wal-clock)

clock_t

用来保存时间的数据类型,在time.h中定义:typedef   long   clock_t;  为长整型

CLOCKS_PER_SEC

用来表示一秒钟会有多少个时钟计时单元,在time.h中定义:#define CLOCKS_PER_SEC ((clock_t)1000)  

 

获取系统运行的时间

 

代码
 
   
CString str,str1;

// 获取系统运行时间

long t = GetTickCount();

str1.Format(
" 系统已运行 %d时 " ,t / 3600000 );

str
= str1;

t
%= 3600000 ;

str1.Format(
" %d分 " ,t / 60000 );

str
+= str1;

t
%= 60000 ;

str1.Format(
" %d秒 " ,t / 1000 );

str
+= str1;

AfxMessageBox(str);

 

 

 

法三  利用CTime类 获取系统时间

 

 
  
CString str;

// 获取系统时间

CTime tm;

tm
= CTime::GetCurrentTime();

str
= tm.Format( " 现在时间是%Y年%m月%d日 %X " );

AfxMessageBox(str);

 

 

 

法四  利用GetLocalTime类 获取系统时间

 

代码
 
   
SYSTEMTIME st;

CString strDate,strTime;

GetLocalTime(
& st);

strDate.Format(
" %4d-%2d-%2d " ,st.wYear,st.wMonth,st.wDay);

strTime.Format(
" %2d:%2d:%2d " ,st.wHour,st.wMinute,st.wSecond);

AfxMessageBox(strDate);

AfxMessageBox(strTime);

 

 

 

 

Author:         SKySeraph

Email/GTalk: zgzhaobo@gmail.com    QQ:452728574

From:         http://www.cnblogs.com/skyseraph/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,请尊重作者的劳动成果。

 

转载于:https://www.cnblogs.com/skyseraph/archive/2010/12/30/1922341.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值