c++ 計算程序運行時間


1.如果只是要計算程序運行的時間,不需要那么復雜。

<windows.h> 中的 GetTickCount() 就是干這個的。  

TimeStart=GetTickCount();

   .......

   TimeEnd=GetTickCount();

   TimeUsed=TimeEnd-TimeStart;

2. #include<stdio.h>

#include<time.h>

#include<conio.h>

int main()

{

time_t stime , etime ;

time( &stime ); /* get start time */

getch(); /* Access */

time( &etime ); /* get end time */

printf( "%ld\n" , etime - stime );

getch();

return 0;

}

3. class CTimer

{

public:

CTimer() {QueryPerformanceFrequency(&m_Frequency); Start();}

void Start() {QueryPerformanceCounter(&m_StartCount);}

double End() {LARGE_INTEGER CurrentCount;QueryPerformanceCounter(&CurrentCount);return double(CurrentCount.LowPart - m_StartCount.LowPart) / (double)m_Frequency.LowPart;}

private:

LARGE_INTEGER m_Frequency;

LARGE_INTEGER m_StartCount;

};  

 

4. VC的話有profile,在鏈接屬性頁勾選profile項,然后profile(在編譯菜單下),各個函數時間都出來了

 

5. #include <iostream>

#include <ctime>

using namespace std;

int max(int x,int y)

{

return (x>y)?x:y;

}

int main()

{

const double begin=(double)clock()/CLK_TCK;

for(int i=10000;i>0;i--)

for(int j=10000;j>0;j--)

max(i,j);

const double end=(double)clock()/CLK_TCK;

cout <<begin<<" "<<end;

return 0;

}

6.要最精確的有

LARGE_INTEGER limtp;

QueryPerformanceFrequency(&limtp);//獲得當前的計數頻率,即每秒進行多少次計數

QueryPerformanceCounter(&limtp);//獲取當前計數次數

基於cpu級的

時間是

(計數獲取計數次數 - 開始獲取計數次數)/(用QueryPerformanceFrequency獲取的limtp.QuadPart)   

    

 

下面列出簡單的例子

#include <ctime> //計時用的頭文件

#include <iostream>

using namespace std;

int main()

{

    time_t start,end,time; /*注意計時所用的變量名稱*/

    /*程序開始執行,開始計時*/

    start=clock();

    /*程序執行過程……*/

    for(int i=0;i<=100000;i++) cout << i << ' ';

    cout << endl;

    /*程序結束執行,結束計時*/

    end=clock();

    time=end-start;//這里的時間是計算機內部時間

    cout << endl << ""time:" << time << endl;

    system("pause");

    return 0;

}

 

其它:

Include head file time.h, though it's a C include file, C++ certainly can use it.
Under C++, you can include <ctime> instead of <time.h>
_____________________________________________________

time.h


@函數名稱:     localtime
函數原型:     struct tm *localtime(const time_t *timer)
函數功能:     返回一個以tm結構表達的機器時間信息
函數返回:     以tm結構表達的時間,結構tm定義如下:
struct  tm{
      int tm_sec;
      int tm_min;
      int tm_hour;
      int tm_mday;
      int tm_mon;
      int tm_year;
      int tm_wday;
      int tm_yday;
      int tm_isdst;
    };
參數說明:     timer-使用time()函數獲得的機器時間
所屬文件:     <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
    time_t timer;
    struct tm *tblock;
    timer=time(NULL);
    tblock=localtime(&timer);
    printf("Local time is: %s",asctime(tblock));
    return 0;
}


@函數名稱:     asctime
函數原型:     char* asctime(struct tm * ptr)
函數功能:     得到機器時間(日期時間轉換為ASCII碼)
函數返回:     返回的時間字符串格式為:星期,月,日,小時:分:秒,年
參數說明:     結構指針ptr應通過函數localtime()和gmtime()得到
所屬文件:     <time.h>

#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
    struct tm t;
    char str[80];
    t.tm_sec=1;
    t.tm_min=3;
    t.tm_hour=7;
    t.tm_mday=22;
    t.tm_mon=11;
    t.tm_year=56;
    t.tm_wday=4;
    t.tm_yday=0;
    t.tm_isdst=0;
    strcpy(str,asctime(&t));
    printf("%s",str);
    return 0;
}


@函數名稱:     ctime
函數原型:     char *ctime(long time)
函數功能:     得到日歷時間
函數返回:     返回字符串格式:星期,月,日,小時:分:秒,年
參數說明:     time-該參數應由函數time獲得
所屬文件:     <time.h>

#include <stdio.h>
#include <time.h>
int main()
{
    time_t t;
    time(&t);
    printf("Today's date and time: %s",ctime(&t));
    return 0;
}


@函數名稱:     difftime
函數原型:     double difftime(time_t time2, time_t time1)
函數功能:     得到兩次機器時間差,單位為秒
函數返回:     時間差,單位為秒
參數說明:     time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得
所屬文件:     <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main()
{
    time_t first, second;
    clrscr();
    first=time(NULL);
    delay(2000);
    second=time(NULL);
    printf("The difference is: %f seconds",difftime(second,first));
    getch();
    return 0;
}


@函數名稱:     gmtime
函數原型:     struct tm *gmtime(time_t  *time)
函數功能:     得到以結構tm表示的時間信息
函數返回:     以結構tm表示的時間信息指針
參數說明:     time-用函數time()得到的時間信息
所屬文件:     <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr="TZ=PST8PDT";
int main()
{
    time_t t;
    struct tm *gmt, *area;
    putenv(tzstr);
    tzset();
    t=time(NULL);
    area=localtime(&t);
    printf("Local time is:%s", asctime(area));
    gmt=gmtime(&t);
    printf("GMT is:%s", asctime(gmt));
    return 0;
}


@函數名稱:     time
函數原型:     time_t time(time_t *timer)
函數功能:     得到機器的日歷時間或者設置日歷時間
函數返回:     機器日歷時間
參數說明:     timer=NULL時得到機器日歷時間,timer=時間數值時,用於設置日歷時間,time_t是一個long類型
所屬文件:     <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
    time_t t;
    t=time();
    printf("The number of seconds since January 1,1970 is %ld",t);
    return 0;
}


@函數名稱:     tzset
函數原型:     void tzset(void)
函數功能:     UNIX兼容函數,用於得到時區,在DOS環境下無用途
函數返回:
參數說明:
所屬文件:     <time.h>

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
    time_t td;
    putenv("TZ=PST8PDT");
    tzset();
    time(&td);
    printf("Current time=%s",asctime(localtime(&td)));
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM