簡體   English   中英

獲取使用std :: chrono花費的平均時間

[英]Get average of time spent using std::chrono

我的功能運行超過一百萬次。 我想通過打印對該函數的10,000次調用的持續時間總和來打印出該函數運行所需時間的持續時間。

在每個函數的開頭,我都有類似以下內容:

int counter = 0;
auto duration_total = 0; //not sure about the type
std::chrono::high_resolution_clock::time_point t1, t2, duration;

t1 = std::chrono::high_resolution_clock::now(); 
Function f(){
  counter++;
}

t2 = std::chrono::high_resolution_clock::now();
duration= std::chrono::duration_cast<std::chrono::nanoseconds>( t2 - t1 ).count();
duration_total += duration;

if(counter %10000 == 0){
      long int average_duration = duration_total/10000;
      duration_total = 0;
      cout << average_duration << "\n";
}

我找不到增加持續時間然后求平均值的方法。

您在啟動時創建一個時鍾,在停止時創建一個時鍾。 從另一個時鍾中減去一個時鍾時,您會得到一個持續時間。 將持續時間除以迭代次數。

例:

#include <chrono>
#include <functional>
#include <iostream>

template<typename T>
auto timeit(size_t iterations, std::function<void()> func_to_test) {
    auto start = std::chrono::high_resolution_clock::now();

    for(size_t i = 0; i < iterations; ++i)
        func_to_test();

    auto end = std::chrono::high_resolution_clock::now();

    return std::chrono::duration_cast<T>(end - start) / iterations;
}

int main() {
    auto dur =
        timeit<std::chrono::microseconds>(10000, [] { system("echo Hello World"); });

    std::cout << dur.count() << " µs\n";
}

如果需要匯總各個運行,請保留要添加到的工期變量。 我正在重用相同的timeit函數,但如果只想運行一次,則可以刪除其中的iteration內容。

int main() {
    std::chrono::microseconds tot{0};
    size_t iterations = 0;

    for(size_t i = 0; i < 10; ++i) {
        // sum up the total time spent
        tot += timeit<decltype(tot)>(1, [] { system("echo Hello World"); });
        ++iterations;
    }
    // divide with the number of iterations
    std::cout << (tot / iterations).count() << " µs\n";
}

如果查看std::chrono::duration<Rep,Period>::count ,您會看到可以使用

int duration = std::chrono::duration_cast<std::chrono::nanoseconds>( t2 - t1 ).count();

(或其他,例如unsigned long ),因為返回值是

此持續時間的滴答數。

在全:

#include <iostream>
#include <chrono>

int main()
{
    int counter = 0;
    auto duration_total = 0; //not sure about the type
    std::chrono::high_resolution_clock::time_point t1, t2;

    t1 = std::chrono::high_resolution_clock::now(); 

    t2 = std::chrono::high_resolution_clock::now();
    int duration = std::chrono::duration_cast<std::chrono::nanoseconds>( t2 - t1 ).count();
    duration_total += duration;

    if(counter %10000 == 0){
          long int average_duration = duration_total/10000;
          duration_total = 0;
          std::cout << average_duration << "\n";
    }
}

Coliru中看到它。

首先,這里的類型是int:

auto duration_total = 0;

您應該執行類似的操作:

auto t1 = std::chrono::steady_clock::now();
//do some work
auto t2 = std::chrono::steady_clock::now();
double duration_in_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count();

請注意,我將持續時間延長了一倍。 然后,您可以更自由地使用持續時間值。

如果您更喜歡納秒:

double duration_in_nanoseconds =  std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM