CPU周期计数作为执行指令数的近似值

时间:2013-04-18 10:30:12

标签: windows profiling cpu-usage

我正在尝试测量在Windows上执行某些代码所花费的CPU周期。在运行上面的代码(Visual C ++ 11)时,我注意到CPU周期可能因运行而异。由于没有明确的I / O,我不知道为什么会发生这种情况。

一般来说,线程花费的CPU周期与执行的指令量之间的关系是什么?我可以使用CPU周期作为其近似值吗?

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <algorithm>
int _tmain(int argc, _TCHAR* argv[])
{
    unsigned __int64 thread_cycle1;
    unsigned __int64 thread_cycle2;

    HANDLE thread_handle = GetCurrentThread();
    QueryThreadCycleTime(thread_handle, &thread_cycle1);

    // Code for profiling
    int a[] = {1,3,4,5,6,7,23,4,2,6,7,8,9};
    std::sort(a, a + sizeof(a) / sizeof(a[0]));

    QueryThreadCycleTime(thread_handle, &thread_cycle2);

    std::cout << thread_cycle2 - thread_cycle1 << " cycles";
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我认为你必须概括太多才能说Cycles~ =#Instructions已执行。不同的指令有不同的延迟。

您可以在以下链接中找到至少针对英特尔®64和IA-32的详细信息:

http://www.intel.co.uk/content/dam/doc/manual/64-ia-32-architectures-optimization-manual.pdf

附录C涉及此类延迟。

至于它们变化的原因,另一条评论将适用,特别是因为缓存未命中会显着改变行为。

相关问题