如何用vc获取系统时间和程序运行时间(How to obtain system time and program running time with vc)_第1页
如何用vc获取系统时间和程序运行时间(How to obtain system time and program running time with vc)_第2页
如何用vc获取系统时间和程序运行时间(How to obtain system time and program running time with vc)_第3页
如何用vc获取系统时间和程序运行时间(How to obtain system time and program running time with vc)_第4页
如何用vc获取系统时间和程序运行时间(How to obtain system time and program running time with vc)_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、如何用vc+获取系统时间和程序运行时间(How to obtain system time and program running time with vc+)Title: how to get system time and program running time with vc+Provenance: the cause of springTime: Mon, 22, Jun, 2009, 17:34:26, +0000Author: xiechunyeAddress: Content:How does Q: get the time? What is the accuracy?A:1

2、use time_t time (time_t * timer) to be accurate to secondsCalculate the time difference using double difftime (time_t, Timer1, time_t, timer0)2 use clock_t clock () to get CPU time accurate to 1/CLOCKS_PER_SEC seconds3, using DWORD GetTickCount () to get the system running time accurate to milliseco

3、nds4, if you use the CTime class of MFC, you can use CTime: GetCurrentTime () to be accurate to seconds5 to obtain high precision time, you can useBOOL QueryPerformanceFrequency (LARGE_INTEGER *lpFrequency) gets the frequency of the counter of the systemBOOL QueryPerformanceCounter (LARGE_INTEGER *l

4、pPerformanceCount) gets the value of the counterThen the difference between the two counters is divided by Frequency to get the time.6, there are also the methods mentioned in Davids article:Multimedia Timer FunctionsThe, following, functions, are, used, with, multimedia, timers.TimeBeginPeriod/time

5、EndPeriod/timeGetDevCaps/timeGetSystemTimeTimeGetTime/timeKillEvent/TimeProc/timeSetEvent has high accuracyThe Q:GetTickCount () function is said to be a millisecond count, is it true, or is it accurate to 55 milliseconds?A:GetTickCount () and GetCurrentTime () are only accurate to 55ms (1 tick is 5

6、5ms). If you want to be accurate to milliseconds, you should use either the timeGetTime function or the QueryPerformanceCounter function. Specific examples can refer to QA001022, use high precision timers in VC+, QA001813, how to achieve accurate timing in Windows and QA004842, timeGetTime function

7、delay is not allowed.How does Q:vc+ get system time and what type of variable is the return value?GetSystemTime return to the Green Weizhi standard timeGetLocalTime, like the above usage, return the time in your area. China returns to Beijing timeVOID GetSystemTime (.LPSYSTEMTIME lpSystemTime of sys

8、tem time structure / / address);Functions can be obtained, where LPSYSTEMTIME is a structInclude: year, month, day, week, hour, minute, second, millisecond.The following is the MSDN document for Time:Compatibility in the Introduction.LibrariesLIBC.LIBSingle, thread, static, library, retail, versionL

9、IBCMT.LIBMultithread, static, library, retail, versionMSVCRT.libimport图书馆Msvcrt.dll,零售版返回值时间以秒为单位返回时间。没有错误返回。参数定时器时间存储位置评论函数返回午夜以来经过的秒数时间(00:00:00),1970年1月1日,通用协调时间,根据系统时钟。返回值存储在由定时器给定的位置。此参数可能是NULL,在这种情况下,返回值不存储。例子c说明了各种时间和日期函数,包括:*时间_ftime CTime asctime*当地时间gmtime mktime _tzset* _strtime _strdate

10、strftime*也是全局变量:* _tzname* /#包括时间。”#包括#包括系统/类型。”#包括系统/概念。”#包括 tm_hour 12)strcpy(ampm,“PM”);今天- tm_hour = 12;如果(今天- tm_hour = = 0)/ *调整如果午夜。* /今天- tm_hour = 12;/ *注意如何使用指针相加跳过前11个*字符和printf是用来修剪终止*人物。* /printf(“12小时时间: T T T T %。8s %s n“,asctime(今天)+ 11、ampm);*打印额外的时间信息。* /_ftime(与tstruct);printf(“加毫

11、秒: T T T u n”,litm);printf(“区域差异:T秒UTC %u n“,tstruct.timezone);printf(“时区名称: T T T T %s n”,_tzname 0 );printf(“夏令: T T T %s n“,tstruct.dstflag?”“是”:“不”;*为圣诞节腾出时间,1993。* /如果(mktime(和圣诞节)!=(time_t)- 1)printf(“圣诞节 T T T T %s n”,asctime(和圣诞节);*使用时间结构来构建定制的时间字符串。* /今天是当地时间(和时间);/ *使用函数建立一个定制的时

12、间字符串。* /strftime(tmpbuf,128,“今天是%,一年中的一年中的日百分比;printf(tmpbuf);输出操作时间:21:51:03操作系统日期:05 / 03 / 94UTC 1/170以来的秒数:768027063Unix时间和日期:星期二可能03 21:51:03 1994协调世界时:我们可以04 04:51:03 199412小时的时间:09:51:03 PM加上毫秒:279UTC 480秒的区域差异:时区名称:日光节约:是的圣诞节坐12月25日12:00:00 1993今天是星期二,1994年5月03日。1。使用CTime类CString str;/ /获取系统

13、时间CTime TM;TM = CTime:getcurrenttime();STR = TM。Format (the time is%Y,%m,%d,%X); MessageBox (STR, NULL, MB_OK).;2: get the system date and time (GetLocalTime) SYSTEMTIME St CString; strDate, strTime; GetLocalTime (&st); strDate.Format (%4d-%2d-%2d, st.wYear, st.wMonth, st.wDay); strTime.Format (%2d:

14、%2d:%2d, st.wHour, st.wMinute, st.wSecond);3. use GetTickCount/ to get the run time long (t1=GetTickCount); / / get the program running time period before the start of (MS) Sleep (500); long (t2=GetTickCount); (); / / run time system has procedures after the end of (MS) str.Format (time:%dms, t2-t1)

15、; / / the difference before and after run time AfxMessageBox (STR); / / get the system running time of long t=GetTickCount (CString); STR, STR1; str1.Format (the system is running%d, t/3600000); str=str1; t%=3600000; str1.Format (%d , t/60000); str+=str1; t%=60000; str1.Format (%d seconds , t/1000);

16、 str+=str1; AfxMessageBox (STR);How to get the running time of a program in VC6.0, which is the cost of the program the number of clock cycles / / C and C+ programming time#include #include using namespace STD; int main (time_t) begin, end; (begin=clock); / / here add your code (end=clock); cout run

17、time: double (end-begin /CLOCKS_PER_SECendl);UNIX time correlation is also standard libraries, which in 1.timegm functions simply convert the struct TM structure into a time_t structure without using time zone information; time_t timegm (struct, TM, *tm);2.mktime uses the time zone information time_

18、t mktime (struct TM *tm);The timelocal function is GNU extended and is equivalent to the POSIX function mktime time_t timelocal (struct, TM, *tm);The 3.gmtime function simply transforms the time_t structure into a struct TM structure, without using time zone information; struct TM * gmtime (const ti

19、me_t *clock);4.localtime uses zone time information struct, TM * Localtime (const, time_t, *clock);1.time gets the time; stime sets the time time_t T; t = time (&t); 2.stime; the parameter should be GMT time; set the local time according to the local time zone; int stime (time_t *tp)3.UTC=true said

20、the 4. amendment of daylight saving time; the time information is used in all GMT time, different systems to be modified by Localtime time into local time; 5. setup is recommended to set the time zone setting; 6. set the time zone you can also change the settings in sysconfig/clock /etc/ ln -fs /usr

21、/share/zoneinfo/xxxx/xxx /etc/localtime and then to effectTime_t can only represent a range of 68 years, that is, mktime can only return 1970-2038 of this range of time_t, see if your system has time_t64, it can indicate a greater range of timeThere are some different things in WindowThe CTime MFC c

22、lass seems to have sealed a class for time.h without extending CTime t = GetCurrentTime ();The SYSTEMTIME structure contains information typedef struct _SYSTEMTIME MS WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; SYSTEMTIME, *PSYSTEM

23、TIME;SYSTEMTIME T1; GetSystemTime (&t1) CTime curTime (T1); WORD Ms = t1.wMilliseconds;SYSTEMTIME sysTm: GetLocalTime (&sysTm);In time.h (_strtime) / / t11 can only use char in windows; _strtime (T); puts (t);-_timeb is defined in SYSTIMEB.H and has four fieldsdstflagmillitmtimetimezoneVoid _ftime (

24、struct, _timeb, *timeptr);Struct _timeb timebuffer; _ftime (&timebuffer); take the current time: the document can go to MS, someone tested, as if only to 16ms!- how to set the current system time of -windowsSYSTEMTIME m_myLocalTime, *lpSystemTime; m_myLocalTime.wYear=2003; m_myLocalTime.wMonth=1; m_

25、myLocalTime.wDay=1; m_myLocalTime.wHour= 0; m_myLocalTime.wMinute=0; m_myLocalTime.wSecond=0; m_myLocalTime.wMilliseconds=0; lpSystemTime=&m_myLocalTime; if (SetLocalTime (lpSystemTime) (SetSystemTime) / / here for either MessageBox (OK!); else MessageBox (Error!);SYSTEMTIME m_myLocalTime, *lpSystem

26、Time; m_myLocalTime.wYear=2003; m_myLocalTime.wMonth=1; m_myLocalTime.wDay=1; lpSystemTime=&m_myLocalTime; if (SetDate (lpSystemTime) (SetSystemTime) / / here for either MessageBox (OK!); else MessageBox (Error!);- clock () function, time after the system starts the millisecond, then divided by CLOC

27、KS_PER_SEC, you can replace the “ second, ” standard C function. Clock_t clock (void);#include clock_t t = clock (); long sec = t / CLOCKS_PER_SEC; he is recording the clock cycle; the implementation does not appear to be accurate and requires experimental verification;It is said that - tc2.The 0 ti

28、me structure contains millisecond information #include, , #include, Int main (void) struct time t;Gettime (&t); printf (The current time is:%2d:%02d:%02d.%02dn, t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund); return 0; time is a structure, which is a member function ti_hund milliseconds. The program can

29、run on TC2.0This is - windows which used to calculate the running time of the function; DWORD = GetTickCount (dwStart);Run your code / / hereDWORD dwEnd = GetTickCount ();(dwEnd-dwStart) is your program running time, in milliseconds, the function is only accurate to 55ms, and the 1 tick is 55ms. -ti

30、meGetTime () is basically equal to GetTickCount (), but is more accurate, DWORD, dwStart = timeGetTime ();Run your code / / hereDWORD dwEnd = timeGetTime ();(dwEnd-dwStart) is your program running time in milliseconds, although the value returned should be Ms, but the legend is only 10ms. -The time

31、function of Borland C+ Builder VCLThe 1. Date returns the TDateTime object containing the current month date information. The function prototype is as follows: System: TDateTime, _fastcall, Date (void);The 2. Time returns the TDateTime object containing the current time information. The function pro

32、totype is as follows: System: TDateTime, _fastcall, Time (void);The 3. Now returns the TDateTime object to obtain the current date and time information. The function prototype is as follows: System: TDateTime, _fastcall, Now (void);4. DatetimeToString TDateTime object to a string object in the speci

33、fied format, the function prototype is as follows: void _fastcall DateTimeToString (AnsiString & Result, const AnsiString; Format, System: TDateTime: DateTime);5., DateToStr converts the TDateTime object (including the current day date) into a string object. The function prototype is as follows: Ans

34、iString, _fastcall, DateToStr (System: TDateTime, Date);6., TimeToStr converts the current date to a string object. The function prototype is as follows: AnsiString, _fastcall, TimeToStr (System: TDateTime, Time);7. DateTimetoStr converts a TDateTime object into a string object,函数原型如下:AnsiString _fa

35、stcall datetimetostr(系统:TDateTime类型);8。strtodate将字符串对象转换为年月日对象,函数原型如下:系统:TDateTime _fastcall strtodate(const AnsiString S);9。strtotime将字符串对象转换时间对象,函数原型如下:系统:TDateTime _fastcall strtotime(const AnsiString S);10.strtodatetime将字符串对象转换为年月日时间对象,函数原型如下:系统:TDateTime _fastcall strtodatetime(const AnsiString

36、 S);11.datetimetosystemtime将TDateTime对象转换为操作系统时间,函数原型如下:无效_fastcall datetimetosystemtime(系统:TDateTime DateTime,_systemtime &;系统时间);12.systemtimetodatetime将操作系统时间转换为TDateTime对象,函数原型如下:系统:TDateTime _fastcall systemtimetodatetime(const _systemtime &;系统时间);-下面是转的一个用汇编的精确计时方法-如何获得程序或者一段代码运行的时间?你可能说有专门的程序

37、测试工具,确实,不过你也可以在程序中嵌入汇编代码来实现。在奔腾CPU内部的指令系统中有一条指令可以获得64位计数器的值,我们可以通过代码两次获取该计数器的值而获得程序或代码运行的时钟周期数,进而通过你的CPU的频率算出一个时钟周期的时间,从而算出程序运行的确切时间。我们通过指令TDSIC来获得CPU内部计数器的值,指令TDSIC返回值放在EDX EAX中,其中EDX中存放64位寄存器中高32位的值,EAX存放第32位的值。下面看看实现的代码:/ /用汇编实现获取一段代码运行的时间#包含iostream 使用名称空间;无效getclocknumber(长的高,长的低);无效getruntime(

38、);main() int长highstart,lowstart,高端、低端;长numhigh,numlow;/ /获取代码运行开始时CPU内部计数器的值_asm RDTSC MOV highstart MOV EAX,EDX lowstart,为(int i = 0;i 100000;i+) for(int i = 0;i 100000;i+) /获取代码结束时CPU内部计数器的值,并减去初值_asm RDTSC MOV高端,EDX Mov LowEnd, eax; two sub acquisition counter is eax, LowStart CMP eax, 0; if the difference is negative 32 low and return, because the second have always than the first large JG L1 neg eax JMP L2 L1: mov numlow eax L2:, SBB EDX, HighStart mov numhigh, EDX / / the two counter value of the difference in a 64 bit integer variable in the first 32 / 32 bit shift le

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论