从托管代码性能调用本机代码
这是一个简单的示例。
CodeDLL.cpp 文件:
extern "C" {
__declspec(dllexport) int __cdecl SimulateGameDLL (int a, int b) {
return a*b; // Calculation in native code
}
}
GameSharp.cs 文件:
static class GameSharp
{
public static class UnsafeNativeMethods
{
const string _dllLocation = "CoreDLL.dll";
[DllImport(_dllLocation)]
public static extern int SimulateGameDLL(int a, int b);
}
}
或
static class GameSharp
{
public static class GameSharpClass
{
public static int SimulateGameDLL(int a, int b) {
return a*b; //Calculation in managed code
}
}
}
第一个:我有一个用 C++ 本机代码编写的 dll 文件,我尝试在托管代码中调用其中的函数。(包装器)
在第二个文件中,我已将其转换到托管代码。 我的代码并不像这样简单。哪一个更好地将我的 c++ 本机代码转换为托管代码或将其称为托管代码。 哪一个更快?为什么?
如果不清楚,请告诉我,我将进一步讨论它。在告诉我之前不要投票结束。 :) 谢谢
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 _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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的代码并不那么简单,我想说这取决于您要转换为非托管代码的代码类型。如果代码与复杂的数字运算有关,也许非托管版本会比托管版本更快,但我认为您应该更多地检查您的算法,而不是编译器如何编译或优化代码。
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.
我相信,当谈到 .NET 的性能时,托管代码和非托管代码之间没有明显的差异。
如果您的非托管代码全部在 .NET 上运行,我会将其转换为托管代码。
我只是认为您不应该担心这个级别的性能瓶颈。如果您从事游戏开发,您应该担心网络和图形(使用着色器)方面的性能。
编辑:
http://msdn.microsoft.com/en-us/library/bb677124 .aspx
“最大执行速度。管理层为程序增加了大约 10% 的开销。” (适用于 Windows 移动设备)
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)