繁体   English   中英

QueryPerformanceCounter返回负数

[英]QueryPerformanceCounter returning negative number

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

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

谁能告诉我我是否正确执行此操作?

您是从开始时间减去结束时间。

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

应该

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

假设我从10秒开始,到30秒结束。 它花了多少时间? 20秒 为了做到这一点,我们将做30 - 10 ; 也就是说,第二次减去第一次。

所以也许您想要:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM