Dll函数调用比普通函数调用更快
Dll function call is faster then a normal function call?
我正在测试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;
}
我只测试函数调用速度,获得地址可以在启动时完成。所以性能损失并不重要
对于一个非常小的函数,区别在于函数返回/清除参数的方式。
然而,这应该不会有太大的区别。我认为编译器意识到你的函数不与resultcalculation做任何事情,并优化它。尝试使用两个不同的变量,然后打印它们的值。
相关文章:
- Visual c ++,使用字符串引用/指针调用 dll 函数
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 加载由 MATLAB Coder 生成的带有函数的 DLL,该函数调用外部函数
- 调用 "project" 函数和调用 DLL 函数之间的区别
- C ++ DLL导入:函数调用返回错误的结果
- 调用 dll 函数时"Run-Time Check Failure #0 - The value of ESP"
- #VALUE!在 64 位 Excel 中调用 DLL 函数 (C++) 时
- 使用 c++ 调用.dll的默认参数的函数
- 从Qt(c ++)调用DLL中的函数
- 如果在调用 DLL 中的函数时没有传递足够的参数,会发生什么情况?
- C++调用 DLL 函数
- MQL4 C++Dll在函数调用中更改字符串参数
- 无法调用 DLL 中的函数
- 从C++调用dll函数
- 从MS访问中调用DLL函数
- 如何从C++DLL中的C#简单函数调用
- 必须在其他线程上调用 DLL 函数
- 在 C++ 中,如何使用不同库中的相同签名调用 DLL 函数
- C++如何从dll函数调用中获得正确的返回
- 如何确定哪个 Windows DLL 用于函数调用