Reputation: 1875
I was testing the speed of an exported function in a DLL and a normal function. How is it possible that an exported function in a DLL is a lot faster?
100000000 function calls in a DLL cost: 0.572682 seconds
100000000 normal function class cost: 2.75258 seconds
This is the function in the DLL:
extern "C" __declspec (dllexport) int example()
{
return 1;
}
This is the normal function call:
int example()
{
return 1;
}
This is how I test it:
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;
}
I only test the function call speed, getting the address can be done at start-up. So the performance lost of that doesn't mater.
Upvotes: 5
Views: 1277
Reputation: 129364
For a very small function, the difference is in the way that a function returns/cleans up arguments.
However, that shouldn't make that much of a difference. I think the compiler realizes that your function doesn't do anything with resultCalcuation and optimizes it away. Try using two different variables and print their value afterwards.
Upvotes: 8