如何用vc 获取系统时间和程序运行时间.doc
《如何用vc 获取系统时间和程序运行时间.doc》由会员分享,可在线阅读,更多相关《如何用vc 获取系统时间和程序运行时间.doc(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、标题:如何用vc+获取系统时间和程序运行时间出处:春天的事业时间:Mon, 22 Jun 2009 17:34:26 +0000作者:xiechunye地址:内容:Q:如何获取时间?精度如何? A:1 使用time_t time( time_t * timer ) 精确到秒计算时间差使用double difftime( time_t timer1, time_t timer0 )2 使用clock_t clock() 得到的是CPU时间 精确到1/CLOCKS_PER_SEC秒3 使用DWORD GetTickCount() 得到的是系统运行的时间 精确到毫秒4 如果使用MFC的CTime类,
2、可以用CTime:GetCurrentTime() 精确到秒5 要获取高精度时间,可以使用 BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)获取系统的计数器的频率 BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值 然后用两次计数器的差除以Frequency就得到时间。6 还有David的文章中提到的方法: Multimedia Timer Functions The following functions are used with m
3、ultimedia timers. timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime timeGetTime/timeKillEvent/TimeProc/timeSetEvent 精度很高 Q:GetTickCount()函数,说是毫秒记数,是真的吗,还是精确到55毫秒?A:GetTickCount()和GetCurrentTime()都只精确到55ms(1个tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数。具体例子可以参考QA00102
4、2 VC+中使用高精度定时器、QA001813 如何在Windows实现准确的定时和QA004842 timeGetTime函数延时不准。Q:vc+怎样获取系统时间,返回值是什么类型的变量呢? GetSystemTime返回的是格林威志标准时间 GetLocalTime,和上面用法一样,返回的是你所在地区的时间,中国返回的是北京时间 VOID GetSystemTime( LPSYSTEMTIME lpSystemTime / address of system time structure ); 函数就可以获得了,其中LPSYSTEMTIME 是个结构体 含:年,月,日,周几,小时,分,秒,
5、毫秒。以下是Time的MSDN文档:Compatibility in the Introduction.LibrariesLIBC.LIBSingle thread static library, retail versionLIBCMT.LIBMultithread static library, retail versionMSVCRT.LIBImport library for MSVCRT.DLL, retail versionReturn Valuetime returns the time in elapsed seconds. There is no error return.P
6、arametertimerStorage location for timeRemarksThe time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which
7、case the return value is not stored. Example/* TIMES.C illustrates various time and date functions including: * time _ftime ctime asctime * localtime gmtime mktime _tzset * _strtime _strdate strftime * * Also the global variable: * _tzname */#include #include #include #include #include void main() c
8、har tmpbuf128, ampm = AM; time_t ltime; struct _timeb tstruct; struct tm *today, *gmt, xmas = 0, 0, 12, 25, 11, 93 ; /* Set time zone from TZ environment variable. If TZ is not set, * the operating system is queried to obtain the default value * for the variable. */ _tzset(); /* Display operating sy
9、stem-style date and time. */ _strtime( tmpbuf ); printf( OS time:tttt%sn, tmpbuf ); _strdate( tmpbuf ); printf( OS date:tttt%sn, tmpbuf ); /* Get UNIX-style time and display as number and string. */ time( <ime ); printf( Time in seconds since UTC 1/1/70:t%ldn, ltime ); printf( UNIX time and date:t
10、tt%s, ctime( <ime ) ); /* Display UTC. */ gmt = gmtime( <ime ); printf( Coordinated universal time:tt%s, asctime( gmt ) ); /* Convert to time structure and adjust for PM if necessary. */ today = localtime( <ime ); if( today-tm_hour 12 ) strcpy( ampm, PM ); today-tm_hour -= 12; if( today-tm_hou
11、r = 0 ) /* Adjust if midnight hour. */ today-tm_hour = 12; /* Note how pointer addition is used to skip the first 11 * characters and printf is used to trim off terminating * characters. */ printf( 12-hour time:tttt%.8s %sn, asctime( today ) + 11, ampm ); /* Print additional time information. */ _ft
12、ime( &tstruct ); printf( Plus milliseconds:ttt%un, tstruct.millitm ); printf( Zone difference in seconds from UTC:t%un, tstruct.timezone ); printf( Time zone name:tttt%sn, _tzname0 ); printf( Daylight savings:ttt%sn, tstruct.dstflag ? YES : NO ); /* Make time for noon on Christmas, 1993. */ if( mkti
13、me( &xmas ) != (time_t)-1 ) printf( Christmastttt%sn, asctime( &xmas ) ); /* Use time structure to build a customized time string. */ today = localtime( <ime ); /* Use strftime to build a customized time string. */ strftime( tmpbuf, 128, Today is %A, day %d of %B in the year %Y.n, today ); printf(
14、 tmpbuf );OutputOS time: 21:51:03OS date: 05/03/94Time in seconds since UTC 1/1/70: 768027063UNIX time and date: Tue May 03 21:51:03 1994Coordinated universal time: Wed May 04 04:51:03 199412-hour time: 09:51:03 PMPlus milliseconds: 279Zone difference in seconds from UTC: 480Time zone name: Daylight
15、 savings: YESChristmas Sat Dec 25 12:00:00 1993Today is Tuesday, day 03 of May in the year 1994.1.使用CTime类 CString str;/获取系统时间CTime tm;tm=CTime:GetCurrentTime();str=tm.Format(现在时间是%Y年%m月%d日 %X);MessageBox(str,NULL,MB_OK);2: 得到系统时间日期(使用GetLocalTime)SYSTEMTIME st;CString strDate,strTime;GetLocalTime(&
16、st);strDate.Format(%4d-%2d-%2d,st.wYear,st.wMonth,st.wDay);strTime.Format(%2d:%2d:%2d,st.wHour,st.wMinute,st.wSecond);3.使用GetTickCount/获取程序运行时间long t1=GetTickCount();/程序段开始前取得系统运行时间(ms)Sleep(500);long t2=GetTickCount();();/程序段结束后取得系统运行时间(ms)str.Format(time:%dms,t2-t1);/前后之差即 程序运行时间AfxMessageBox(str)
17、;/获取系统运行时间long t=GetTickCount();CString str,str1;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);如何在VC6.0中得到一个程序的运行时间,也就是这个程序耗费的时钟周期数/ C和C+的时间编程 #include #include using namespace std; int main() tim
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 如何用vc 获取系统时间和程序运行时间 何用 vc 获取 系统 时间 程序 运行
限制150内