VC/MFC中计算程序/系统运行时间
法一 利用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);
还没有评论,来说两句吧...