我需要两种方式的分析功能。第一个是代码花费的总时间,第二个是不依赖于cpu-freq并休眠在代码中的单位。 (我们的软件需要使用自己的语言/解释器进行性能分析,它可以在Windows上运行)
我的问题是第二个问题。
GetThreadTimes
的结果取决于cpu-freq,并且不准确(10-15ms),请参见更多信息:Why GetThreadTimes is wrong? Kalmbachnet QueryThreadCycleTime
也取决于实现。 (以及我测试过的睡眠)也可以查看更多信息:What
does QueryThreadCycleTime actually count? OldNewThing QueryPerformanceCounter
是一个精确的计数器,但是cpu-freq-change会更改结果,并且还包括睡眠。我可能想做什么?或还有其他方法吗?视觉工作室配置文件如何/做什么?
注意:我知道我的问题似乎是重复的。我试图评论一些对相同问题的旧答案(例如: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