c ++ GetSystemTime在调试时返回0,但在单步执行断点时返回正确的输出

时间:2015-04-01 18:09:47

标签: c++ time

我试图获得一些函数运行所需的时间,但是当我使用调试器运行时,我得到零。当我插入断点并逐步执行程序时,我得到了正确的输出。这是下面的代码。

vector<int> nums = { 5, 8, 1, 3, 9, 45, 12, 4 };
SYSTEMTIME startTime;
SYSTEMTIME endTime;
Sorter<int> s;
s.setData(nums);
GetSystemTime(&startTime);
s.bubbleSort();
s.combSort();
s.insertionSort();
GetSystemTime(&endTime);
auto diff = endTime.wMilliseconds - startTime.wMilliseconds;
cout << "Took " << diff << endl;

1 个答案:

答案 0 :(得分:0)

以下是计算时差的方法:

_int64 Delta(SYSTEMTIME st1, SYSTEMTIME st2) 
{
  union timeunion 
  {
      FILETIME fileTime;
      ULARGE_INTEGER ul;
  };

  timeunion ft1;
  timeunion ft2;

  SystemTimeToFileTime(&st1, &ft1.fileTime);
  SystemTimeToFileTime(&st2, &ft2.fileTime);

  return ft2.ul.QuadPart - ft1.ul.QuadPart;
}