如何用vc 获取系统时间和程序运行时间.doc_第1页
如何用vc 获取系统时间和程序运行时间.doc_第2页
如何用vc 获取系统时间和程序运行时间.doc_第3页
如何用vc 获取系统时间和程序运行时间.doc_第4页
如何用vc 获取系统时间和程序运行时间.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

标题:如何用vc+获取系统时间和程序运行时间出处:春天的事业时间:Mon, 22 Jun 2009 17:34:26 +0000作者:xiechunye地址:/read.php/612.htm内容: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类,可以用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 multimedia timers. timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime timeGetTime/timeKillEvent/TimeProc/timeSetEvent 精度很高 Q:GetTickCount()函数,说是毫秒记数,是真的吗,还是精确到55毫秒?A:GetTickCount()和GetCurrentTime()都只精确到55ms(1个tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数。具体例子可以参考QA001022 VC+中使用高精度定时器、QA001813 如何在Windows实现准确的定时和QA004842 timeGetTime函数延时不准。Q:vc+怎样获取系统时间,返回值是什么类型的变量呢? GetSystemTime返回的是格林威志标准时间 GetLocalTime,和上面用法一样,返回的是你所在地区的时间,中国返回的是北京时间 VOID GetSystemTime( LPSYSTEMTIME lpSystemTime / address of system time structure ); 函数就可以获得了,其中LPSYSTEMTIME 是个结构体 含:年,月,日,周几,小时,分,秒,毫秒。以下是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.ParametertimerStorage 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 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() char 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 system-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:ttt%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_hour = 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. */ _ftime( &tstruct ); printf( Plus milliseconds:ttt%un, litm ); 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( mktime( &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( 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 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(&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);/获取系统运行时间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() time_t begin,end; begin=clock(); /这里加上你的代码 end=clock(); coutruntime: double(end-begin)/CLOCKS_PER_SECendl; unix时间相关,也是标准库的这些在1.timegm函数只是将struct tm结构转成time_t结构,不使用时区信息;time_t timegm(struct tm *tm); 2.mktime使用时区信息time_t mktime(struct tm *tm);timelocal 函数是GNU扩展的与posix函数mktime相当time_t timelocal (struct tm *tm);3.gmtime函数只是将time_t结构转成struct tm结构,不使用时区信息;struct tm * gmtime(const time_t *clock);4.localtime使用时区信息struct tm * localtime(const time_t *clock);1.time获取时间,stime设置时间time_t t;t = time(&t);2.stime其参数应该是GMT时间,根据本地时区设置为本地时间;int stime(time_t *tp)3.UTC=true 表示采用夏时制;4.文件的修改时间等信息全部采用GMT时间存放,不同的系统在得到修改时间后通过localtime转换成本地时间;5.设置时区推荐使用setup来设置;6.设置时区也可以先更变/etc/sysconfig/clock中的设置 再将ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效time_t只能表示68年的范围,即mktime只能返回1970-2038这一段范围的time_t看看你的系统是否有time_t64,它能表示更大的时间范围Window里面的一些不一样的CTime MFC类,好像就是把time.h封了个类,没扩展CTime t = GetCurrentTime();SYSTEMTIME 结构包含毫秒信息typedef struct _SYSTEMTIME WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; SYSTEMTIME, *PSYSTEMTIME;SYSTEMTIME t1;GetSystemTime(&t1) CTime curTime(t1); WORD ms = t1.wMilliseconds;SYSTEMTIME sysTm;:GetLocalTime(&sysTm);在time.h中的_strtime() /只能在windows中用char t11;_strtime(t);puts(t);-_timeb定义在SYSTIMEB.H,有四个fieldsdstflagmillitmtimetimezonevoid _ftime( struct _timeb *timeptr );struct _timeb timebuffer; _ftime( &timebuffer );取当前时间:文档讲可以到ms,有人测试,好象只能到16ms!-如何设定当前系统时间-windowsSYSTEMTIME m_myLocalTime,*lpSystemTime; m_myLocalTime.wYear=2003; m_myLocalTime.wMonth=1; m_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( )也不行 MessageBox(OK !); else MessageBox(Error !); SYSTEMTIME m_myLocalTime,*lpSystemTime;m_myLocalTime.wYear=2003;m_myLocalTime.wMonth=1;m_myLocalTime.wDay=1;lpSystemTime=&m_myLocalTime;if( SetDate(lpSystemTime) ) /此处换成 SetSystemTime( )也不行 MessageBox(OK !); else MessageBox(Error !); -用clock()函数,得到系统启动以后的毫秒级时间,然后除以CLOCKS_PER_SEC,就可以换成“秒”,标准c函数。clock_t clock ( void );#include clock_t t = clock();long sec = t / CLOCKS_PER_SEC;他是记录时钟周期的,实现看来不会很精确,需要试验验证;-据说tc2.0的time结构含有毫秒信息#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 是一个结构体, 其中成员函数 ti_hund 是豪秒。上程序可以在tc2.0运行-这个是windows里面常用来计算程序运行时间的函数;DWORD dwStart = GetTickCount();/这里运行你的程序代码DWORD dwEnd = GetTickCount();则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位这个函数只精确到55ms,1个tick就是55ms。-timeGetTime()基本等于GetTickCount(),但是精度更高DWORD dwStart = timeGetTime();/这里运行你的程序代码DWORD dwEnd = timeGetTime();则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位 虽然返回的值单位应该是ms,但传说精度只有10ms。-Borland C+ Builder VCL的时间函数1. Date返回TDateTime对象,包含当前的年月日信息,函数原型如下:System:TDateTime _fastcall Date(void);2. Time返回TDateTime对象,包含当前的时间信息,函数原型如下:System:TDateTime _fastcall Time(void);3. Now返回TDateTime对象,获取当前的日期和时间信息,函数原型如下:System:TDateTime _fastcall Now(void);4. DatetimeToString将TDateTime对象转换为指定格式的字符串对象,函数原型如下:void _fastcall DateTimeToString(AnsiString &;Result, const AnsiString Format,System:TDateTime DateTime);5. DateToStr将TDateTime对象(包含当前年月日信息)转换为字符串对象,函数原型如下:AnsiString _fastcall DateToStr(System:TDateTime Date);6. TimeToStr将当前日期转换为字符串对象,函数原型如下:AnsiString _fastcall TimeToStr(System:TDateTime Time);7. DateTimetoStr将TDateTime对象转换为字符串对象,函数原型如下:AnsiString _fastcall DateTimeToStr(System:TDateTime DateTime);8. StrToDate将字符串对象转换为年月日对象,函数原型如下:System:TDateTime _fastcall StrToDate(const AnsiString S);9. StrToTime将字符串对象转换时间对象,函数原型如下:System:TDateTime _fastcall StrToTime(const AnsiString S);10.StrToDateTime将字符串对象转换为年月日时间对象,函数原型如下:System:TDateTime _fastcall StrToDateTime(const AnsiString S);11.DateTimeToSystemTime将TDateTime对象转换为操作系统时间,函数原型如下:void _fastcall DateTimeToSystemTime(System:TDateTime DateTime, _SYSTEMTIME &;SystemTime);12.SystemTimeToDateTime将操作系统时间转换为TDateTime对象,函数原型如下:System:TDateTime _fastcall SystemTimeToDateTime(const _SYSTEMTIME &;SystemTime);-下面是转的一个用汇编的精确计时方法-如何获得程序或者一段代码运行的时间?你可能说有专门的程序测试工具,确实,不过你也可以在程序中嵌入汇编代码来实现。在Pentium的指令系统中有一条指令可以获得CPU内部64位计数器的值,我们可以通过代码两次获取该计数器的值而获得程序或代码运行的时钟周期数,进而通过你的cpu的频率算出一个时钟周期的时间,从而算出程序运行的确切时间。我们通过指令TDSIC来获得cpu内部计数器的值,指令TDSIC返回值放在EDX:EAX中,其中EDX中存放64位寄存器中高32位的值,EAX存放第32位的值.下面看看实现的代码:/用汇编实现获取一段代码运行的时间 #includeusing namespace std;

温馨提示

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

评论

0/150

提交评论