-1

嘿,我试图计算函数执行所需的时间我这样做是这样的: Timer.cpp

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

主文件

#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

2 回答 2

2

您正在从开始时间减去结束时间。

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

应该

cout << "\nTime Taken : " << query.time2 - query.time1
于 2013-03-20T18:13:25.540 回答
1

假设我在 10 秒开始做某事,然后在 30 秒结束。花了多长时间?20 秒。要做到这一点,我们会这样做30 - 10;即第二次减去第一次。

所以也许你想要:

cout << "\nTime Taken : " << (query.time2 - query.time1);
于 2013-03-20T18:13:39.593 回答