欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++下程序運(yùn)行時(shí)間的四種常用計(jì)時(shí)方法總結(jié)

 更新時(shí)間:2024年09月23日 10:00:53   作者:ucliaohh  
這篇文章主要介紹了C++下程序運(yùn)行時(shí)間的四種常用計(jì)時(shí)方法,介紹了幾種常用的計(jì)時(shí)方法,包括低精度的clock()和GetTickCount(),以及高精度的gettimeofday()和QueryPerformanceCounter(),需要的朋友可以參考下

前言

記錄下當(dāng)前時(shí)間start,調(diào)用程序fun(),再記錄一下時(shí)間end。

前后時(shí)間一減(start-end)就得到程序的運(yùn)行時(shí)間了。

首先介紹最常用的,但兩種精度不是很高(>=10ms)的方法:clock()和GetTickCount()

一、clock()

C系統(tǒng)調(diào)用方法,所需頭文件ctime/time.h,即windows和linux都可以使用。

1、clock()返回類型為clock_t類型

2、clock_t實(shí)際為long 類型, typedef long clock_t

3、clock() 函數(shù),返回從 開啟這個(gè)程序進(jìn)程 到 程序中調(diào)用clock()函數(shù) 時(shí)之間的CPU時(shí)鐘計(jì)時(shí)單元(clock tick)數(shù)(掛鐘時(shí)間),返回單位是毫秒

4、可以用常量CLOCKS_PER_SEC, 這個(gè)常量表示每一秒(per second)有多少個(gè)時(shí)鐘計(jì)時(shí)單元

#include <time.h>   //引入頭文件
int main()
{
clock_t start,end;   //定義clock_t變量
start = clock();     //開始時(shí)間

fun()  //需計(jì)時(shí)的函數(shù)

end = clock();   //結(jié)束時(shí)間
cout<<"time = "<<double(end-start)/CLOCKS_PER_SEC<<"s"<<endl;  //輸出時(shí)間(單位:s)
}

二、GetTickCount()

GetTickCount()是一個(gè)Windows API,所需頭文件為<windows.h>。

返回從操作系統(tǒng)啟動(dòng)到現(xiàn)在所經(jīng)過的毫秒數(shù)(ms),精確度有限,跟CPU有關(guān),一般精確度在16ms左右,最精確也不會(huì)精確過10ms。它的返回值是DWORD,當(dāng)統(tǒng)計(jì)的毫妙數(shù)過大時(shí),將會(huì)使結(jié)果歸0,影響統(tǒng)計(jì)結(jié)果.

#include <windows.h>   //引入頭文件
int main()
{
    DWORD t1,t2;
    t1 = GetTickCount();

    fun()  //需計(jì)時(shí)的函數(shù)

    t2 = GetTickCount();
    cout<<"time = "<<((t2-t1)*1.0/1000)<<endl;  //輸出時(shí)間(單位:s)
}

接下來是兩種高精度的計(jì)時(shí)方法:gettimeofday() 和 QueryPerformanceCounter()

三、QueryPerformanceCounter()

QueryPerformanceCounter()是一個(gè)Windows API,所需頭文件為<windows.h>

這個(gè)函數(shù)返回高精確度性能計(jì)數(shù)器的值,它可以以微妙為單位計(jì)時(shí).但是QueryPerformanceCounter() 確切的精確計(jì)時(shí)的最小單位是與系統(tǒng)有關(guān)的,

所以,必須要查詢系統(tǒng)以得到QueryPerformanceCounter()返回的嘀噠聲的頻率. QueryPerformanceFrequency() 提供了這個(gè)頻率值,返回每秒嘀噠聲的個(gè)數(shù).

#include <windows.h>   //引入頭文件
int main()
{
    LARGE_INTEGER t1,t2,tc;
    QueryPerformanceFrequency(&tc);
    QueryPerformanceCounter(&t1);

    fun()  //需計(jì)時(shí)的函數(shù)

    QueryPerformanceCounter(&t2);
    time=(double)(t2.QuadPart-t1.QuadPart)/(double)tc.QuadPart; 
    cout<<"time = "<<time<<endl;  //輸出時(shí)間(單位:s)
}

四、gettimeofday()

gettimeofday() linux環(huán)境下的計(jì)時(shí)函數(shù),int gettimeofday ( struct timeval * tv , struct timezone * tz ),gettimeofday()會(huì)把目前的時(shí)間由tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r(shí)區(qū)的信息則放到tz所指的結(jié)構(gòu)中.

//timeval結(jié)構(gòu)定義為:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
//timezone 結(jié)構(gòu)定義為:
struct timezone{
int tz_minuteswest; /*和Greenwich 時(shí)間差了多少分鐘*/
int tz_dsttime; /*日光節(jié)約時(shí)間的狀態(tài)*/
};

這個(gè)函數(shù)獲取從1970年1月1日到現(xiàn)在經(jīng)過的時(shí)間和時(shí)區(qū)(UTC時(shí)間),(按照linux的官方文檔,時(shí)區(qū)已經(jīng)不再使用,正常應(yīng)該傳NULL)。

調(diào)用代碼:

#include <sys/time.h>   //引入頭文件
int main()
{
    struct timeval t1,t2;
    double timeuse;
    gettimeofday(&t1,NULL);

    fun();

    gettimeofday(&t2,NULL);
    timeuse = (t2.tv_sec - t1.tv_sec) + (double)(t2.tv_usec - t1.tv_usec)/1000000.0;

    cout<<"time = "<<timeuse<<endl;  //輸出時(shí)間(單位:s)
}

還有一種C系統(tǒng)調(diào)用方法--time(),但是精度很低(秒級(jí)),不建議使用,這里就稍微帶下用法。

    time_t start,stop;
    start = time(NULL);
    fun();
    stop = time(NULL);

附帶三種計(jì)算Python的代碼塊或程序的運(yùn)行時(shí)間的方法

方法一

import datetime
start = datetime.datetime.now()
run_function():
    # do something
end = datetime.datetime.now()
print (end-start

方法二

import time
start = time.time()
run_function()
end = time.time()
print str(end)

方法三

import time
start = time.clock()
run_function()
end = time.clock()
print str(end-start)

其中,方法二的精度比較高。方法一基本上是性能最差的。這個(gè)其實(shí)是和系統(tǒng)有關(guān)系的。一般我們推薦使用方法二和方法三。我的系統(tǒng)是Ubuntu,也就是Linux系統(tǒng),方法二返回的是UTC時(shí)間。 在很多系統(tǒng)中time.time()的精度都是非常低的,包括windows。

總概來講,在 Unix 系統(tǒng)中,建議使用 time.time(),在 Windows 系統(tǒng)中,建議使用 time.clock()。

總結(jié)

到此這篇關(guān)于C++下程序運(yùn)行時(shí)間的四種常用計(jì)時(shí)方法的文章就介紹到這了,更多相關(guān)C++程序運(yùn)行時(shí)間計(jì)時(shí)方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論