#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的也應該一樣,只不過位元不同罷了