2013-03-20 152 views
-1

嘿我嘗試計數功能需要多長時間來執行 我這樣做是這樣的: Timer.cppQueryPerformanceCounter的返回負數

long long int Timer :: clock1() 
{ 
    QueryPerformanceCounter((LARGE_INTEGER*)&time1); 
    return time1; 
} 
long long int Timer :: clock2() 
{ 
    QueryPerformanceCounter((LARGE_INTEGER*)&time2); 
    return time2; 
} 

的main.cpp

#include "Timer.h" //To allow the use of the timer class. 

Timer query; 

void print() 
{ 
    query.clock1(); 
    //Loop through the elements in the array. 
    for(int index = 0; index < num_elements; index++) 
    { 
     //Print out the array index and the arrays elements. 
     cout <<"Index: " << index << "\tElement: " << m_array[index]<<endl; 
    } 
    //Prints out the number of elements and the size of the array. 
    cout<< "\nNumber of elements: " << num_elements; 
    cout<< "\nSize of the array: " << size << "\n"; 
    query.clock2(); 
    cout << "\nTime Taken : " << query.time1 - query.time2; 
} 

任何人都可以告訴我,如果我這樣做是否正確?

+4

'query.time1 - query.time2'將是負面的,如果'time2'是較大的,對不對? – 2013-03-20 18:12:56

+0

正確,但我會如何實現兩個變量的差異? – Becca 2013-03-20 18:13:59

+0

'query.time2 - query.time1' – 2013-03-20 18:14:41

回答

1

您正在從開始時間減去結束時間。

cout << "\nTime Taken : " << query.time1 - query.time2; 

應該

cout << "\nTime Taken : " << query.time2 - query.time1 
1

比方說,我在10秒內開機的東西,它完成在30秒。它花了多少時間? 20秒。爲了得到那個,我們會做30 - 10;也就是第二次減去第一次。

因此,也許你想:

cout << "\nTime Taken : " << (query.time2 - query.time1);