C++ QueryPerformanceCounter返回负数
嘿,我正在计算函数执行所需的时间 我是这样做的: 定时器.cppC++ QueryPerformanceCounter返回负数,c++,performancecounter,C++,Performancecounter,嘿,我正在计算函数执行所需的时间 我是这样做的: 定时器.cpp long long int Timer :: clock1() { QueryPerformanceCounter((LARGE_INTEGER*)&time1); return time1; } long long int Timer :: clock2() { QueryPerformanceCounter((LARGE_INTEGER*)&time2); return time
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;
}
main.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;
}
#包括“Timer.h”//以允许使用Timer类。
定时器查询;
作废打印()
{
query.clock1();
//循环遍历数组中的元素。
for(int index=0;index cout您正在从开始时间减去结束时间
cout << "\nTime Taken : " << query.time1 - query.time2;
cout假设我从10秒开始,30秒结束。需要多长时间?20秒。要得到它,我们要做30-10
;也就是说,第二次减去第一次
所以,也许你想要:
cout << "\nTime Taken : " << (query.time2 - query.time1);
coutquery.time1-query.time2
如果time2
较大,则会为负值,对吗?正确,但我如何才能实现这两个变量的差异呢?query.time2-query.time1