繁体   English   中英

如何使用QueryPerformanceCounter来测试现有代码的性能?

[英]How to use QueryPerformanceCounter to test performance of existing code?

我在Visual Studio中有一个现有项目,其中有一个调用功能文件的主文件。 我还按照Microsoft指南中的步骤创建了CodeTimer.cpp,并将其与必要的标头一起放置在与代码和函数相同的目录中。

问题是,我不知道如何链接它们。 该解决方案构建良好,所有三个文件都没有错误。 但是当我按CTRL-F5组合键时,出于明显的原因(我没有将CodeTimer链接到主体),我只是看到了主体的输出。

这是我的CodeTimer:

#include "stdafx.h"
#include <tchar.h>
#include <windows.h>

using namespace System;

int _tmain(int argc, _TCHAR* argv[])
{
    __int64 ctr1 = 0, ctr2 = 0, freq = 0;
    int acc = 0, i = 0;

    // Start timing the code.
    if (QueryPerformanceCounter((LARGE_INTEGER *)&ctr1) != 0)
    {
        // Code segment is being timed.
        for (i = 0; i<100; i++) acc++;

        // Finish timing the code.
        QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);

        Console::WriteLine("Start Value: {0}", ctr1.ToString());
        Console::WriteLine("End Value: {0}", ctr2.ToString());

        QueryPerformanceFrequency((LARGE_INTEGER *)&freq);

        Console::WriteLine("QueryPerformanceFrequency : {0} per Seconds.", freq.ToString());
        Console::WriteLine("QueryPerformanceCounter minimum resolution: 1/{0} Seconds.", freq.ToString());
        Console::WriteLine("ctr2 - ctr1: {0} counts.", ((ctr2 - ctr1) * 1.0 / 1.0).ToString());
        Console::WriteLine("65536 Increments by 1 computation time: {0} seconds.", ((ctr2 - ctr1) * 1.0 / freq).ToString());
    }
    else
    {
        DWORD dwError = GetLastError();
        Console::WriteLine("Error value = {0}", dwError.ToString());
    }

    // Make the console window wait.
    Console::WriteLine();
    Console::Write("Press ENTER to finish.");
    Console::Read();

    return 0;
}

NVM修复了它。 只需在我的代码下的main()函数中添加_tmain()的主体,即可完全使用CodeTimer.cpp文件。 这是电源冲突(一个项目中有多个电源,编译器会自动输出项目中优先级最高的一个)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM