开发者

Call Native Code from Managed Code Performance

It is a simple sample.

CodeDLL.cpp File:

extern "C" {
    __declspec(dllexport) int __cdecl SimulateGameDLL (int a, int b) {
              return a*b; // Calculation in native code
      }
}

GameSharp.cs File:

static class GameSharp
{
    public static class UnsafeNativeMethods
    {
        const string 开发者_如何学Python_dllLocation = "CoreDLL.dll";

        [DllImport(_dllLocation)]
        public static extern int SimulateGameDLL(int a, int b);
    }
}

or

static class GameSharp
{
    public static class GameSharpClass
    {       
        public static int SimulateGameDLL(int a, int b) {
             return a*b; //Calculation in managed code
        }
    }
}

The first one : I have a dll file that written in c++ native code and i try to call function in it in the managed code.(wrapper)

In the second one i have convert it to Managed Code. My Code is not as simple as this.Which One is better Convert My c++ native code to Managed Or Call it form Managed Code. Which one is faster? Why?

If this is not clear, please tell me i will discuss it more.don't vote it to close before tell me. :) thanks


Since your code is not as simple as that, I would tell it depends on what type of code are you converting to unmanaged code. If the code has to do with complex numeric operations, maybe the unmanaged version will be faster than the managed, BUT I think you should review your ALGORITHM more than how the code is compiled or optimized by the compiler.


I believe when it comes to performance in .NET there isn't a noticeable difference between managed and unmanaged code.

I would convert your unmanaged code to managed if its all running on .NET anyway.

I just think you shouldn't be worried about a performance bottleneck at this level. If your doing game development, you should be worried about performance when it comes to networking and graphics (use shaders).

EDIT:

http://msdn.microsoft.com/en-us/library/bb677124.aspx

"Maximum speed of execution. The managed layer adds around 10% overhead to the program." (for windows mobile)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜