2016-08-07 1 views
0
time_t start; 
time_t no_infection; 
bool time_already_set = 0; 
bool infected_or_not = 0; 
int not_infected_t = 0; 

私はこの変数をstructに入れています。オブジェクトの開始点と終了点に印を付けたいのですが、その差を計算するのです。スタートのために ctimeを使用してプログラムの開始点と終了点をマークするにはどうすればよいですか?

void bacteria::set_no_infection() { 
    if (infected_or_not == 0) 
     no_infection = clock(); 
} 

私は、その時の変数は、私は同じようにそれをテストするメインプログラムで

double bacteria::get_time() { 
    if (infected_or_not == 1) 
     return ((clock() - start)/CLOCKS_PER_SEC); 
    else 
     return -1; 
} 
int bacteria::get_no_infection() { 
    if (infected_or_not = 0) 
     return ((clock() - no_infection)/CLOCKS_PER_SEC); 
    else 
     return -1; 
} 

のget関数を使用してテストプログラム中に変化しないようだ

void bacteria::set_time() { 
    if (infected_or_not == 1 && time_already_set!=1) { 
     start = clock(); 
     time_already_set = 1; 
    } 
} 

持っていますこれは:

while (1) { 
    for (int i = 0; i < b.size() - 1; i++) { 
     bactpop[i].set_no_infection(); 
     bactpop[i].inf(phagepop[i], bactpop[i], p); 
     bactpop[i].kill_the_bacteria(b, i); 
     cout << "  " << b[i].start << " " << b[i].no_infection << endl; 
    } 
    cout << p.size() << " " << b.size() << endl; 
} 
+0

] .set_no_infection();ファクタポップ[i]、bactpop [i]、p); bactpop [i] .inf(phagepop [i]、bactpop [i]、p);\t \t \t b35)。 \t \t \t cout << "" << b [i] .start << "" << b [i] .no_infection << endl; \t \t \t \tcout << p.size()<< "" << b.size()<< endl; \t} –

答えて

0

私はあなたの関数が1秒間に数回呼び出されていると思いますが、clock()/ CLOCKS_PER_SECは秒単位で値を返しますので、呼び出しは同じ秒で実行されるため、0になります。

たとえば、マイクロ秒を持つgettimeofdayを使用するか、C++ 11を使用する場合はstd :: high_resolution_clock :: now()を使用します。 ; - ; { \t \t \t bactpop [I(Iは+ +1 iがb.sizeを()

関連する問題