1

単位を印刷せずに時間だけを取得する方法はあるのだろうか:

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

boost::chrono::milliseconds sumGlobal;

int main() {
    boost::chrono::high_resolution_clock::time_point t1 ;
    boost::chrono::high_resolution_clock::time_point t2 ;

    for (i=0;i<10;i++)
    { 
        t1 = boost::chrono::high_resolution_clock::now();
        f(); //to waste time   
        t2 = boost::chrono::high_resolution_clock::now();
        sumGlobal += (boost::chrono::duration_cast<boost::chrono::milliseconds>(t2-t1)); 
    }          

    std::cout << sumGlobal << "\n";        
}

出力は次のとおりです。

123 milliseconds 

のみ取得したい

123

解決策はありますか?

4

1 に答える 1

2

millisecondsdurationクラス テンプレートのテンプレート インスタンス化です。

typedef duration<boost::int_least64_t, milli> milliseconds;

ストリーム出力演算子<<は、durationクラスに対してオーバーロードされています。

template <class CharT, class Traits, class Rep, class Period>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d);

ケースのテンプレート パラメーターを指定すると、提供されたバージョンでは、デフォルトで単位「ミリ秒」がテキストとして追加されます。

ただし、期間クラスには、指定された整数型 (あなたの場合は型パラメーターcount) として指定された単位 (ミリ秒の場合) で期間を返すメソッドがあります。repboost::int_least64_t

constexpr rep count() const;

その整数を独自の形式で出力できます(この場合は純粋な数値のみ):

std::cout << sumGlobal.count() << std::endl;
于 2015-06-18T12:17:04.777 に答える