2013-03-20 5 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' были больше, верно? –

+0

Правильно, но как еще я мог бы достичь разницы в двух переменных? – Becca

+0

'query.time2 - query.time1' –

ответ

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);