用C+度量函數的執行時間我想知道某個函數在我的C+程序中執行所需的時間。linux..之后,我想做一個速度比較。我看到了幾個時間函數,但最后得到的是Boost。編年史:process_user_cpu_clock, captures user-CPU time spent by the current process現在,我不清楚我是否使用了上述功能,我會得到CPU在這個功能上花費的唯一時間嗎?其次,我找不到任何使用上述功能的例子。有誰能幫我使用上面的功能嗎?P.S:現在,我在用std::chrono::system_clock::now()獲得時間的秒,但這給我不同的結果,因為不同的CPU負載,每次。
3 回答
慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
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;}注:
Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
#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 關注
- 507 瀏覽
添加回答
舉報
0/150
提交
取消
