0

Visual C++2010 Express と WinXP を使用して次のコードを実行すると、「for ループ」は次のように一貫して実行されます。

31ms で 25000 行を読み取る, 62ms で 25000 行を読み取る, 46ms で 25000 行を読み取る, 62ms で 25000 行を読み取る, 46ms で 25000 行を読み取る, 46ms で 25000 行を読み取る

ただし、Windows 7 Home Edition で Visual C++2010 Express を使用してコンパイルすると、for ループは次のように実行されます。

25000 行を 62 ミリ秒で読み取る, 25000 行を 530 ミリ秒で読み取る, 25000 行を 514 ミリ秒で読み取る, 25000 行を 514 ミリ秒で読み取る, 25000 行を 514 ミリ秒で読み取る, 25000 行を 530 ミリ秒で読み取る

「for loop」が Windows 7 で最初に t ミリ秒実行される理由を理解しようとしていますが、その後の実行では 10 xt ミリ秒にジャンプします。XPでは一貫してtミリ秒で実行されます。これは、Windows 7 のビルド/セットアップに特有のものか、コードの基本的なものかもしれません。

私は最近 C++ のプログラミングを始めました。Windows 7 環境で何が起こっているのかを理解するための支援を本当に感謝しています。

#include <iostream>
#include <string>
#include <vector>
#include "Elapsed.h"

using namespace std;

void readfile(){
    Elapsed time1;
    vector<string> lines;
    lines.reserve(50000);
    string s = "this is a string this is a longer string ";
    for(int i = 0; i<25000; i++) lines.push_back(s);
    cout<<"Read "<<lines.size()<<" lines in "<<time1().total_milliseconds()<<"ms\n";
}

int main(){
    readfile();
    readfile();
    readfile();
    readfile();
    readfile();
    readfile();
    system("PAUSE");
}

#include <boost/date_time.hpp> 
// Purpose: track elapsed time from constructor or reset
class Elapsed {
    boost::posix_time::ptime m_when;
public:
    static boost::posix_time::ptime now(){return boost::posix_time::microsec_clock::universal_time();}
    Elapsed(): m_when( now() )
    {
    } // end constructor

    void reset() { m_when = now(); }

    boost::posix_time::time_duration operator()() const {
        return now() - m_when;
    } //end operator()
};// end class
4

1 に答える 1

1

次のように時間を計算する必要があります。

boost::posix_time::time_duration span = time1();
cout << "Read " << lines.size() << " lines in "<< 
     span.total_milliseconds() << "ms\n";

operator<<にはシーケンス ポイントが含まれていないため、タイマーに への出力の一部を含めることができcout、IO タイミングがまったく予測できないことに注意してください。

于 2013-03-08T20:00:54.933 に答える