5

DLL 内のエクスポートされた関数と通常の関数の速度をテストしていました。DLL 内のエクスポートされた関数がはるかに高速である可能性があるのはなぜですか?

100000000 function calls in a DLL cost: 0.572682 seconds
100000000 normal function class cost: 2.75258 seconds

これは DLL 内の関数です。

extern "C" __declspec (dllexport) int example()
{
    return 1;
}

これは通常の関数呼び出しです。

int example()
{
    return 1;
}

これは私がそれをテストする方法です:

int main()
{
    LARGE_INTEGER frequention;
    LARGE_INTEGER dllCallStart,dllCallStop;
    LARGE_INTEGER normalStart,normalStop;
    int resultCalculation;

    //Initialize the Timer
    ::QueryPerformanceFrequency(&frequention);
    double frequency = frequention.QuadPart;
    double secondsElapsedDll = 0;
    double secondsElapsedNormal = 0;

    //Load the Dll
    HINSTANCE hDll = LoadLibraryA("example.dll");

    if(!hDll)
    {
        cout << "Dll error!" << endl;
        return 0;
    }

    dllFunction = (testFunction)GetProcAddress(hDll, "example");

    if( !dllFunction )
    {
        cout << "Dll function error!" << endl;
        return 0;
    }

    //Dll
    resultCalculation = 0;
    ::QueryPerformanceCounter(&dllCallStart);
    for(int i = 0; i < 100000000; i++)
        resultCalculation += dllFunction();
    ::QueryPerformanceCounter(&dllCallStop);

    Sleep(100);

    //Normal
    resultCalculation = 0;
    ::QueryPerformanceCounter(&normalStart);
    for(int i = 0; i < 100000000; i++)
        resultCalculation += example();
    ::QueryPerformanceCounter(&normalStop);

    //Calculate the result time
    secondsElapsedDll = ((dllCallStop.QuadPart - dllCallStart.QuadPart) / frequency);
    secondsElapsedNormal = ((normalStop.QuadPart - normalStart.QuadPart) / frequency);

    //Output
    cout << "Dll: " << secondsElapsedDll << endl; //0.572682
    cout << "Normal: " << secondsElapsedNormal << endl; //2.75258

    return 0;
}

関数呼び出し速度のみをテストします。アドレスの取得は起動時に実行できます。したがって、それによって失われたパフォーマンスは重要ではありません。

4

1 に答える 1

8

非常に小さな関数の場合、違いは、関数が引数を返す/クリーンアップする方法にあります。

ただし、それほど大きな違いはないはずです。コンパイラは、関数が resultCalcuation で何もしないことを認識し、それを最適化すると思います。2 つの異なる変数を使用してみて、後でそれらの値を出力してください。

于 2013-01-11T08:53:25.783 に答える