测量cpu周期或其他不取决于cpu频率并且不计算睡眠时间/周期的单位吗? WinAPI C ++

时间:2019-10-16 07:55:48

标签: c++ debugging winapi profiling

我需要两种方式的分析功能。第一个是代码花费的总时间,第二个是不依赖于cpu-freq并休眠在代码中的单位。 (我们的软件需要使用自己的语言/解释器进行性能分析,它可以在Windows上运行)

我的问题是第二个问题。

我可能想做什么?或还有其他方法吗?视觉工作室配置文件如何/做什么?

注意:我知道我的问题似乎是重复的。我试图评论一些对相同问题的旧答案(例如:Another question)以获得更好的答案,但是我的评论在1-2天后被删除。 (请参阅:meta for comments deleted

编辑 :(我的QueryThreadCycleTime测试代码)

static void foo()
{
  for (int i = 0; i < 3; i++)
  {
    Sleep(20);

    for (int x = 0; x < 1000; x++)
      x = x + 1 - 1;
  }
}

static void testCycles()
{
  HANDLE hThread = nullptr;
  ::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentThread(), ::GetCurrentProcess(), &hThread, 0, false, DUPLICATE_SAME_ACCESS);

  std::vector<ULONG64> results;
  results.resize(7);

  for (auto &tElapsed : results)
  {
    ULONG64 tStart = 0;
    ::QueryThreadCycleTime(hThread, &tStart);

    foo();

    ULONG64 tEnd = 0;
    ::QueryThreadCycleTime(hThread, &tEnd);

    tElapsed = tEnd - tStart;
  }

  ::CloseHandle(hThread);
}

有结果;

带睡眠(20)

在线程中 123383 192271 128028 208208 277983 223377 155222

在主线程中 191616 120002 126258 125267 141934 204753 125243

with Sleep(1000)

在线程中 121595 143863 182068 307464 388448 342315 468244

在主线程中 289568 256256 348599 359328 234065 167849 299888

0 个答案:

没有答案
相关问题