简体   繁体   中英

Disadvantages of calling unmanaged code from a managed C# app

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.

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. Eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM