計算程序運行時間的方法

1

這個是windows裏面經常使用來計算程序運行時間的函數;
DWORD dwStart = GetTickCount();
//這裏運行你的程序代碼
DWORD dwEnd = GetTickCount();
則(dwEnd-dwStart)就是你的程序運行時間, 以毫秒爲單位
這個函數只精確到55ms,1個tick就是55ms。html

#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char* argv[])
{
DWORD start, end;

start = GetTickCount();
for(int i=0;i<1000;i++)
cout<<"you are a good child!"<<endl;   //your code
end = GetTickCount()-start;
cout<<end<<endl;
    return 0;
}

2
timeGetTime()基本等於GetTickCount(),可是精度更高
DWORD dwStart = timeGetTime();

//這裏運行你的程序代碼

DWORD dwEnd = timeGetTime();

則(dwEnd-dwStart)就是你的程序運行時間, 以毫秒爲單位
雖然返回的值單位應該是ms,但傳說精度只有10ms。ios

#include <iostream>
#include <windows.h>
#pragma comment(lib,"winmm.lib")windows

using namespace std;
int main(int argc, char* argv[])
{
DWORD start, end;

start = timeGetTime();
for(int i=0;i<100;i++)
cout<<"you are a good child!"<<endl;
end = timeGetTime()-start;
cout<<end<<endl;
    return 0;
}

3
用clock()函數,獲得系統啓動之後的毫秒級時間,而後除以CLOCKS_PER_SEC,就能夠換成「秒」,標準c函數。
clock_t clock ( void );

#include <time.h>
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是記錄時鐘週期的,實現看來不會很精確,須要試驗驗證;

4
#include<iostream>
#include<ctime> //<time.h>
using   namespace   std;
int   main()
{
    time_t   begin,end; 函數

    double duration;
    begin=clock();
    //這裏加上你的代碼
    end=clock(); 工具

    duration=double(end-begin)/CLOCKS_PER_SEC;
    cout<<"runtime:   "<<duration<<endl;
}

5
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);

6
下面是轉的一個用匯編的精確計時方法
---------------------------------------------------------------------------------------
如何得到程序或者一段代碼運行的時間?你可能說有專門的程序測試工具,確實,不過你也能夠在程序中嵌入彙編代碼來實現。

在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的也應該同樣,只不過位數不一樣罷了測試

相關文章
相關標籤/搜索