Tôi đã thử nghiệm tốc độ của một hàm được xuất trong một DLL và một hàm bình thường. Làm thế nào có thể là một chức năng xuất khẩu trong một DLL là nhanh hơn rất nhiều?Dll gọi hàm nhanh hơn sau đó gọi hàm bình thường?
100000000 function calls in a DLL cost: 0.572682 seconds
100000000 normal function class cost: 2.75258 seconds
Đây là chức năng trong DLL:
extern "C" __declspec (dllexport) int example()
{
return 1;
}
Đây là chức năng bình thường gọi:
int example()
{
return 1;
}
Đây là cách tôi thử nghiệm nó:
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;
}
Tôi chỉ kiểm tra tốc độ gọi hàm, ge tting địa chỉ có thể được thực hiện lúc khởi động. Vì vậy, hiệu suất bị mất mà không mater.
kết quả là gì nếu bạn loại bỏ 'extern" C "'? – stijn
Tôi không tin rằng vì * chức năng bình thường * này sẽ được gạch chân nhưng chức năng từ DLL không thể được gạch chân. Vì vậy, tôi nghĩ rằng bạn phải làm một cái gì đó không thích hợp. Bạn đang sử dụng bản phát hành bản phát hành? – Nawaz
Bạn đã biên dịch cả hai dự án như thế nào? Một số tùy chọn tối ưu hóa nhất định có thể quyết định không gọi hàm nào cả, nhanh hơn. –