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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,请尊重作者的劳动成果。
作者:skyseraph
出处:http://www.cnblogs.com/skyseraph/
更多精彩请直接访问SkySeraph个人站点:http://skyseraph.com//
Email/GTalk: zgzhaobo@gmail.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。