VC獲取系統時間、程序運行時間

 

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<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t begin,end;
begin=clock();
//這裏加上你的代碼
end=clock();
cout<<"runtime: "<<double(end-begin)/CLOCKS_PER_SEC<<endl;
}  
  


unix時間相關,也是標準庫的
這些在<time.h>
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 t[11];
_strtime(t);
puts(t);

------------------------------------------------------------------------------
_timeb定義在SYS/TIMEB.H,有四個fields
dstflag
millitm
time
timezone

void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
   _ftime( &timebuffer );
取當前時間:文檔講可以到ms,有人測試,好象只能到16ms!


-------------------------------------------------------------------------
如何設定當前系統時間---windows
SYSTEMTIME 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 <time.h>
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是記錄時鐘週期的,實現看來不會很精確,需要試驗驗證;

---------------------------------------------------------------------------
據說tc2.0的time結構含有毫秒信息
#include   <stdio.h>
#include   <dos.h>

int main(void)
{
   struct time t;

   gettime(&t);
   printf("The current time is: %2d:%02d:%02d.%02d/n",
          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位的值.
下面看看實現的代碼:
//用匯編實現獲取一段代碼運行的時間
#include<iostream>

using namespace std;
void GetClockNumber (long high, long low);
void GetRunTime();

int main()
{  

long HighStart,LowStart,HighEnd,LowEnd;
long numhigh,numlow;
//獲取代碼運行開始時cpu內部計數器的值
__asm        
{
RDTSC
mov HighStart, edx
mov LowStart, eax
}
for(int i= 0; i<100000; i++ )
{
       for(int i= 0; i<100000; i++ )
   {
    
   }

}
//獲取代碼結束時cpu內部計數器的值,並減去初值
    __asm
{
RDTSC
mov HighEnd, edx
Mov LowEnd, eax
;獲取兩次計數器值得差
sub eax, LowStart
cmp eax, 0    ; 如果低32的差爲負則求返,因爲第二次取得永遠比第一次的大
jg   L1
neg   eax
jmp   L2
      L1: mov numlow, eax
      L2: sbb edx, HighStart
mov numhigh, edx

}
    //把兩個計數器值之差放在一個64位的整形變量中
    //先把高32位左移32位放在64的整形變量中,然後再加上低32位
__int64 timer =(numhigh<<32) + numlow;
     //輸出代碼段運行的時鐘週期數
     //以頻率1.1Gcpu爲例,如果換計算機把其中的1.1改乘其它即可,因爲相信大家的cpu都應該在1G以上 ^_^
cout<< (double) (timer /1.1/1000000000) << endl;
return 0;
}


   這樣通過一條簡單的彙編指令就可以獲得程序或一段代碼的大概時間,不過並不能得到運行的確切時間,因爲即使去掉中間的循環,程序也會有個運行時間,
因爲在第一次取得計數器的值後,有兩條彙編指令mov HighStart, edx    mov LowStart, eax這兩條指令當然也有運行時間 ,當然你可以減去這兩條指令的運行時間(在1.1G的機子上是3e-8s),這樣會更精確一點。
如果你要確切知道程序的運行時間,專業的測試軟件肯定會更好一點,不過好像一般沒有必要獲取除非專門的要求的程序。
不過能DIY一個也是不錯的,不管有沒有,最起碼你可以學到在VC++中如何嵌入彙編代碼以及如何使用32位的寄存器,其實和16位的寄存器一樣使用,將來64的也應該一樣,只不過位數不同罷了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章