I have external .DLL file with fast assembler code inside. What is the best way to call functions in this .DLL file to get best performance?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
The only way to answer this question is to time both options, a task which is trivially easy. Making performance predictions without timing is pointless.
Since we don't have your code, only you can answer your question.
I think DLLImport and LoadLibrary have different goals. If you use native .dll, you should use DllImport. If you use .NET assembly, you should use LoadAssembly.
Actually, you can dynamically load native assembly too, see this example: dynamically-calling-an-unmanaged-dll-from-.net
Assuming your target platform is the same as said native dll. You can use DLLImport to pinvoke LoadLibrary and use LoadLibrary to load the native dll into your process. Then use DllImport to pinvoke GetProcAddress.
Then you can define delegates for all the methods exported in said dll that you want to call.
Next you use the Marshal.GetDelegateForFunctionPointer to set your delegate from GetProcAddress.
You create a static class that does this stuff once in the constructor. Then you can call your delegates to invoke the native exported functions in the dll, without having DllImport on everything. Much cleaner, and I'm pretty sure it's a lot faster and will probably completely bypass before mentioned parameter checks.
SO you would have a slow initialization, but once loaded, would run fast imo. Haven't tested this.
Here's a blog on it from my source.
http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx
Your DLL might be in python or c++, whatever , do the same as follow.
This is your DLL file in C++.
header:
Source code file
Take a look at the following C# code: