Lau*_*nce 5 c++ windows dll performance
我正在测试DLL中的导出函数的速度和正常函数.如何在DLL中导出的函数更快?
100000000 function calls in a DLL cost: 0.572682 seconds
100000000 normal function class cost: 2.75258 seconds
Run Code Online (Sandbox Code Playgroud)
这是DLL中的函数:
extern "C" __declspec (dllexport) int example()
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这是正常的函数调用:
int example()
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这是我测试它的方式:
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;
}
Run Code Online (Sandbox Code Playgroud)
我只测试函数调用速度,获取地址可以在启动时完成.因此,性能损失并不重要.
对于一个非常小的函数,区别在于函数返回/清除参数的方式.
但是,这不应该产生那么大的差别.我认为编译器意识到你的函数不会对resultCalcuation做任何事情并将其优化掉.尝试使用两个不同的变量,然后打印它们的值.
| 归档时间: |
|
| 查看次数: |
957 次 |
| 最近记录: |