DWORD start_time=GetTickCount(); {...} DWORD end_time=GetTickCount(); DWORD Subtime = (end_time-start_time); int k = 0;
在調試中,常常須要計算某一段代碼的執行時間,下面給出兩種經常使用的方式:ios
第一種:使用GetTickCount函數編程
#include<iostream> #include<windows.h> int main() { DWORD start_time=GetTickCount(); { //此處爲被測試代碼 } DWORD end_time=GetTickCount(); cout<<"The run time is:"<<(end_time-start_time)<<"ms!"<<endl;//輸出運行時間 return 0; }
GetTickCount函數返回從系統運行到如今所經歷的時間(類型爲DWORD),單位爲ms,由於DWORD表示範圍的限制,因此使用此種方法存在限制,即系統的運行時間的ms表示不能超出windows
DWORD的表示範圍。函數
第二種:使用clock()函數測試
#include<iostream> #include<time.h> int main() { clock_t start_time=clock(); { //被測試代碼 } clock_t end_time=clock(); cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//輸出運行時間 return 0; }
clock_t,clock()定義於time.h中,clock()返回從程序運行時刻開始的時鐘週期數,類型爲long.CLOCKS_PER_SEC定義了每秒鐘包含多少了時鐘單元數,由於計算ms,因此spa
*1000。unix
由上面分析可知,用clock()函數計算運行時間,表示範圍必定大於GetTickCount()函數,因此,建議使用clock()函數。調試
==========================================================================code
1.使用CTime類
CString str;
//獲取系統時間
CTime tm;
tm=CTime::GetCurrentTime();
str=tm.Format("如今時間是%Y年%m月%d日 %X");
MessageBox(str,NULL,MB_OK);
2: 獲得系統時間日期(使用GetLocalTime)orm
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。--------------------------------------------------------------------------------