2017-08-15 26 views
-2

在下面的代碼中,如何測量總經過時間function1()function3()C++測量重複調用的函數的時間

在實際情況中,function1()function3()散落在複雜的程序中的任何地方。我需要一些可以使用函數名稱來啓動和停止一個timer,所以最終我可以打印出每個函數總共需要多長時間。

請注意,所需的時間不是所需的時間,而是function1()總計時間貶值function1()這是所謂的無處不在多次是需要的價值。

int function1() 
{ 
    int i = 1; 
    i++; 
    return i; 
} 
int function2() 
{ 
    int i = 1; 
    i++; 
    return i; 
} 
int function3() 
{ 
    int i = 1; 
    i++; 
    return i; 
} 
int main(int argc, char *argv[]) 
{ 
    size_t t = 0; 

    while (t < 1000000) 
    { 
     // I want something like startTimer(function1) 
     function1(); 
     // I want something like pauseTimer(function1) 
     function2(); 
     // I want something like startTimer(function3) 
     function3(); 
     // I want something like pauseTimer(function3) 
    } 
    // printTimer() here will print the total elapsed time of function 1 and 
    //function 3 
    getchar(); 
    return 0; 
} 
+2

除了你的例子甚至沒有編譯,你有沒有嘗試過任何東西?我不明白什麼是問題。你可以使用'std :: chrono'中的函數來實現這個功能。 – user0042

+0

對不起。代碼不是我寫的,而是我面對的真實情況的反映。我編輯過它來編譯它。我使用std :: map進行定時器管理,但開銷太大。我確實在核心部分使用了std :: chrono。當發生一次功能時,很容易測量單個功能的時間。當功能分散到各處並且我必須測量總時間時很困難 –

+4

您可能需要一個分析工具。 – user0042

回答

1

運行探查可能是做到這一點的最好辦法,但如果你不能這樣做,因爲某些原因,你可以做到以下幾點:

1)臨時重命名功能1()的執行到例如function1_implementation()(或類似)

2)寫一個新的臨時執行功能1()是這樣的:

static unsigned long long totalMicrosInFunction1 = 0; 

// Returns current system-clock-time, in microseconds 
static unsigned long long get_current_time_in_microseconds() 
{ 
    // This may not be the best way to implement this function; see below 
    struct timeval tv; 
    gettimeofday(&tv, NULL); 
    return ((unsigned long long)tv.tv_sec)*1000000 + tv.tv_usec; 
} 

int function1() 
{ 
    unsigned long long startTime = get_current_time_in_microseconds(); 
    int ret = function1_implementation(); // call the real code! 
    unsigned long long endTime = get_current_time_in_microseconds(); 

    totalMicrosInFunction1 += (endTime-startTime); 
    return ret; 
} 

3)做同樣的伎倆對於要時間的任何其他職責。

...然後重新編譯你的程序,並在main()結尾處打印出totalMicrosInFunction1的當前值。

請注意,上述get_current_system_time_in_microseconds()的實現可能不是您用例的最佳實現;如果您使用C++ 11,則可以使用std::chrono::high_resolution_clock來代替此目的;否則你可以在windows下使用特定於操作系統的API,例如QueryPerformanceCounter()

+0

如果我使用時間分析工具,當我的程序有超過1000個功能,並且那裏只有10個我感興趣的功能,我可以選擇僅查看這10個功能嗎?大概; –

+0

大概;它當然取決於你使用的工具。 –

1

假設您使用單個CPU模塊在PC上運行,您可以使用rdtscp指令並獲取滴答數。積累蜱蟲。完成後,將刻度轉換爲時間,然後完成。

看鏈接:https://msdn.microsoft.com/en-us/library/bb385235.aspx如何在Windows上執行rdtscp。

這是假設你只有一個線程。

現在,創建一個變量「unsigned __int64 totalTicks = 0;」在文件範圍。

功能1將被寫爲如下:

int function1() 
{ 
    unsigned int arg; 
    unsigned __int64 startTicks = __rdtscp(&arg); // arg is not used 
    int ret = function1_implementation(); // call the real code! 
    unsigned __int64 endTicks = __rdtscp(&arg); 
    totalTicks += (endTicks - startTicks); 

    return ret; 
} 

Tick是CPU時鐘速率的倒數,所以2.5GHz的CPU將具有在每秒2.5十億蜱。

或者,您可以創建一個Timer類,它具有我描述的核心功能 - 創建start()和stop()方法以返回兩個值,並使用elapsed()方法返回delta。在任何函數的開頭調用start(),並在結尾調用elapsed()。然後,任何需要唯一定時器的函數都可以使用不同的Timer實例。

這會給現代CPU帶來亞納秒的分辨率。如果你的功能小而快,使用單調時間答案可能不會給你足夠的分辨率。這也是使用分析器的問題,因爲它們通常以10ms的時鐘速率(Linux上的gprof)進行統計學取樣,所以您將得到準確的計數,但只能估計所花費的時間。