Windows平台Sleep(0), YieldProcessor()性能测试

测试环境:

windows 10, Inter i7 7700K

测试标准:

for循环进行1亿次的Sleep(0)及YieldProcessor()

代码:

const int TEST_COUNT = 100000000;
void DoSleepTest()
{
    DWORD beginTestTime = GetTickCount();
    for (int i = 0; i < TEST_COUNT; i++)
        Sleep(0);

    DWORD usedTime = GetTickCount() - beginTestTime;
    printf("End sleep test, test times:%d, usedTime:%d\n", TEST_COUNT, usedTime);
}

void DoCPURelaxTest(bool useAsm)
{
    DWORD beginTestTime = GetTickCount();
    for (int i = 0; i < TEST_COUNT; i++)
    {
        if (useAsm)
        {
            __asm {
                rep nop
            }
        }
        else
        {
            YieldProcessor();
        }
    }

    DWORD usedTime = GetTickCount() - beginTestTime;
    printf("End cpu-relax test(use asm:%s), test times:%d, usedTime:%d\n", useAsm ? "true" : "false", TEST_COUNT, usedTime);
}

int main(int argc, char *argv[])
{
    DoSleepTest();
    DoCPURelaxTest(true);
    DoCPURelaxTest(false);

    getchar();
    return 0;
}

测试结果:

测试结果

你可能感兴趣的:(Windows平台Sleep(0), YieldProcessor()性能测试)