簡體   English   中英

Boost Chrono的process_real_cpu_clock的負持續時間

[英]Negative duration with Boost Chrono's process_real_cpu_clock

我正在嘗試使用Boost.Chrono來衡量我的代碼的CPU時間。 我的C ++代碼如下。 問題是我有時會得到負的持續時間作為輸出。 當我使用“ process_real_cpu_clock”時,會發生這種情況。 當我使用“ steady_clock”時,問題不會出現。 我取得的一些輸出如下:

第一次實施時間:360毫秒

第二次實施所需時間:-3284.97毫秒


第一次實施時間:360毫秒

第二次實施時間:1010毫秒


第一次實施的時間:-3924.97毫秒

第二次實施時間:1010毫秒

只有第二個是預期的。 我猜問題出在持續時間的溢出,但是在第三個輸出中,第一個實現的時間應該是第二個實現的時間的1/3左右,如果我能看到第二個實現的時間,那么就應該沒有溢出。 (我正在使用Boost 1.54,並在VirtualBox上使用Ubuntu)

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <boost/chrono.hpp>
#include <boost/chrono/process_cpu_clocks.hpp>

int main() {

int x,y,result,runs;
srand(time(NULL));
runs=1e7;


boost::chrono::process_real_cpu_clock::time_point start, start2; 
boost::chrono::process_real_cpu_clock::time_point end, end2;

start= boost::chrono::process_real_cpu_clock::now();

    for (int i=0;i<runs;i++) {

        x=rand() %100 +1;
        y=rand() %100 +1;
        auto dummy=x*y;
        result=(result+dummy)%50;
        } 
end=boost::chrono::process_real_cpu_clock::now();
boost::chrono::process_real_cpu_clock::duration diff=end-start;

start2= boost::chrono::process_real_cpu_clock::now();

    for (int i=0;i<(3*runs);i++) {

        x=rand() %100 +1;
        y=rand() %100 +1;
        auto dummy=x*y;
        result=(result+dummy)%50;

        } 
end2=boost::chrono::process_real_cpu_clock::now();

boost::chrono::process_real_cpu_clock::duration diff2=end2-start2;

     std::cout << "time for 1st implemention:"<<boost::chrono::duration <double, boost::milli> (diff).count()<< " ns" << std::endl;
     std::cout << "time for 2nd implementation:"<<boost::chrono::duration <double, boost::milli> (diff2).count()<< " ns" << std::endl;


return 0;
}

我懷疑您錯誤地使用了boost::milli (這只是Boost Ratio的通用Boost Ratio )。

您想對chrono中的單位使用duration_cast

std::cout << "time for 1st implemention:" << duration_cast<nanoseconds>(diff).count() << " ns\n";
std::cout << "time for 2nd implementation:" << duration_cast<nanoseconds>(diff2).count() << " ns\n";

這是我的看法(選擇毫秒作為可讀性):

生活在Coliru

#include <iostream>
#include <boost/chrono.hpp>
#include <boost/chrono/process_cpu_clocks.hpp>

using namespace boost::chrono;

template <typename R, typename P>
    static std::ostream& operator<<(std::ostream& os, duration<R,P> const& d) {
        return os << boost::chrono::duration_cast<boost::chrono::milliseconds>(d).count() << "ms";
    }

template <typename Clock = boost::chrono::process_cpu_clock, typename F, typename... Args>
nanoseconds bench(F&& f, Args&&... args) {
    auto start = Clock::now();

    volatile auto force = std::forward<F>(f)(std::forward<Args>(args)...);
    (void) force;

    return Clock::now() - start;
}

long long foo(int runs) {
    long long result = 0;
    for (int i = 0; i < runs; i++) {
        int x = rand() % 100 + 1;
        int y = rand() % 100 + 1;
        auto dummy = x * y;
        result = (result + dummy) % 50;
    }

    return result;
}

int main() {
    srand(time(NULL));

    std::cout << "1st method: " << bench(foo, 1e7)   << "\n";
    std::cout << "2nd method: " << bench(foo, 3e7) << "\n";
}

打印

1st method: 340ms
2nd method: 1010ms

暫無
暫無

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

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