用C+度量函數(shù)的執(zhí)行時間我想知道某個函數(shù)在我的C+程序中執(zhí)行所需的時間。linux..之后,我想做一個速度比較。我看到了幾個時間函數(shù),但最后得到的是Boost。編年史:process_user_cpu_clock, captures user-CPU time spent by the current process現(xiàn)在,我不清楚我是否使用了上述功能,我會得到CPU在這個功能上花費的唯一時間嗎?其次,我找不到任何使用上述功能的例子。有誰能幫我使用上面的功能嗎?P.S:現(xiàn)在,我在用std::chrono::system_clock::now()獲得時間的秒,但這給我不同的結(jié)果,因為不同的CPU負載,每次。
3 回答

青春有我
TA貢獻1784條經(jīng)驗 獲得超8個贊
std::chrono::high_resolution_clock
<chrono>
#include <iostream>#include <chrono>using namespace std;using namespace std::chrono;void function(){ long long number = 0; for( long long i = 0; i != 2000000; ++i ) { number += 5; }}int main(){ high_resolution_clock::time_point t1 = high_resolution_clock::now(); function(); high_resolution_clock::time_point t2 = high_resolution_clock::now(); auto duration = duration_cast<microseconds>( t2 - t1 ).count(); cout << duration; return 0;}
注:

大話西游666
TA貢獻1817條經(jīng)驗 獲得超14個贊
#include <iostream>#include <ctime> // time_t#include <cstdio>void function(){ for(long int i=0;i<1000000000;i++) { // do nothing }}int main(){time_t begin,end; // time_t is a datatype to store time values.time (&begin); // note time before executionfunction();time (&end); // note time after executiondouble difference = difftime (end,begin); printf ("time taken for function() %.2lf seconds.\n", difference );return 0;}
- 3 回答
- 0 關注
- 384 瀏覽
添加回答
舉報
0/150
提交
取消