开发者

Disadvantages of calling unmanaged code from a managed C# app

开发者 https://www.devze.com 2022-12-14 15:34 出处:网络
What are the drawbacks to calling functions in a native library from a C# application? What can I expect for a performance hit? The functions are basic engineering computations and are nothing too com

What are the drawbacks to calling functions in a native library from a C# application? What can I expect for a performance hit? The functions are basic engineering computations and are nothing too complicated. We could rewrite them in C# some day.开发者_Python百科


P/Invoke has an overhead of about 10-30 x86 instructions per call. How significant this is depends on how often you cross the boundary, and how much time you spend on the other side. For example, if your computations take say 10 microseconds to run, then the additional 10-30 instructions will be insignificant compared to the cost of the computation. If your computation is trivial and is called a lot, then the overhead could be significant enough to justify porting it now, or creating an unmanaged wrapper function that performs the repeated calls (so as to minimise the number of times you cross the boundary).

The main drawback is not performance, but trust requirements. E.g. if you call native code then you can't be run from a network drive or intranet site (at least not without additional configuration), because these sources are by default less trusted than the local machine.


Another point worth noting is that unmanaged code will be shared between all AppDomains in a process. Relevant if you have a process with multiple AppDomains, such as an ASP.NET worker process.

0

精彩评论

暂无评论...
验证码 换一张
取 消