2014-03-27 22 views
0

我有一組布爾值數組,每個布爾值代表一個數字。我正在用for循環打印每個truefor(unsigned long long l = 0; l<numt; l++) if(primes[l]) cout << l << endl;numt是數組的大小,等於超過1000000.控制檯窗口需要30秒才能打印出所有的值,但我在程序中放置的計時器顯示僅爲37微秒。我如何等待所有的值在我的程序屏幕上完成打印,以便在我的時間中包含這些值。檢查攻擊者需要多長時間打印

+0

http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem –

+0

我實際上還沒有嘗試一個解決方案,但@HansPassant。我在問如何等待conhost完成打印所有的值。 – nimsson

回答

1

試試這個:

#include <windows.h> 
... 

int main() { 
    //init code 

    double startTime = GetTickCount(); 

    //your loop 

    double timeNeededinSec = (GetTickCount() - startTime)/1000.0; 
} 
+0

這不同於ctime時間() – nimsson

1

剛剛在的ctime的防守,因爲它提供了相同的結果與GetTickCount的:

#include <ctime>  

int main() 
{ 
    ... 
    clock_t start = clock(); 
    ... 
    clock_t end = clock(); 

    double timeNeededinSec = static_cast<double>(end - start)/CLOCKS_PER_SEC; 
    ... 
} 

更新:

,是其中一種時間()但在這種情況下,我們可能會失去一些精度(〜1秒),因爲結果在幾秒鐘內。

#include <ctime>  

int main() 
{ 
    time_t start; 
    time_t end; 
    ... 

    time(&start); 
    ... 
    time(&end); 

    int timeNeededinSec = static_cast<int>(end-start); 
} 

在簡單的例子,他們兩人的結合會告訴你在結果的差異。在我的測試中,我只看到點之後的差異。

+0

確定,但時間()不起作用。 – nimsson