在下面的代碼中,如何測量總經過時間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;
}
除了你的例子甚至沒有編譯,你有沒有嘗試過任何東西?我不明白什麼是問題。你可以使用'std :: chrono'中的函數來實現這個功能。 – user0042
對不起。代碼不是我寫的,而是我面對的真實情況的反映。我編輯過它來編譯它。我使用std :: map進行定時器管理,但開銷太大。我確實在核心部分使用了std :: chrono。當發生一次功能時,很容易測量單個功能的時間。當功能分散到各處並且我必須測量總時間時很困難 –
您可能需要一個分析工具。 – user0042