简体   繁体   English

管理与C#的非托管物理引擎

[英]Managed Vs Unmanaged Physics Engines for C#

anyone tried BEPU Physic Engine ? 有人试过BEPU Physic Engine吗? http://bepuphysics.codeplex.com/ http://bepuphysics.codeplex.com/

It's a fully managed physic engine written in C# ... I know it mostly used for XNA ( XBOX and WP7 Projects ) Because Unmanaged Codes are not Allowed. 它是一个用C#编写的完全托管的物理引擎......我知道它主要用于XNA(XBOX和WP7项目),因为不允许使用非托管代码。

But what I want to know is how a fully Managed Physic Engine is compared to a P/Invoked One ( For example tao.ODE ) in Windows Environment ( in term of performance ) ? 但我想知道的是如何将完全托管的物理引擎与Windows环境中的P / Invoked One(例如tao.ODE)进行比较(在性能方面 )?

In other words which method makes more overhead, fully managed code or P/Invoke Wrapper around unmanaged Engines like ODE or PhysX in a Real Project? 换句话说,哪个方法在Real Project中围绕非托管引擎(如ODE或PhysX)进行更多开销,完全托管代码或P / Invoke Wrapper?

I cannot comment on the specific physics engines, however I can offer some experience in writing high performance code (both unmanaged and managed). 我不能评论具体的物理引擎,但是我可以提供一些编写高性能代码(非托管和托管)的经验。

A few years ago I worked on a piece of simulation software written in Delphi and ported to .NET (before my arrival I might say). 几年前,我研究了一个用Delphi编写的移植到.NET的仿真软件(在我到达之前我可能会说)。 It was pure managed code and computed ion-trajectories for mass spectrometer simulations. 它是用于质谱仪模拟的纯托管代码和计算离子轨迹。 The code involved numerical integration, differentiation, N-body electrostatic charge calculations so was certainly testing the CPU. 代码涉及数值积分,微分,N体静电荷计算,所以当然是在测试CPU。

I found through various experiments trying to find the highest performance that some C++ versions of the simulation routines could be beaten by well optimized C# code. 我发现通过各种实验试图找到最高性能,一些C ++版本的仿真程序可以被优化的C#代码打败。

By well optimized I mean reduction of new operators (re-use of objects), caching, object pooling, use of structs where possible, minimizing method calls where possible, moving method calls to static / sealed where possible, minimize number of parameters passed to methods, compile in release, x64, detached from debugger. 通过良好优化,我的意思是减少新运算符(重用对象),缓存,对象池,尽可能使用结构,尽可能减少方法调用,尽可能将方法调用移动到static / sealed ,最小化传递给的数量方法,在发行版中编译,x64,与调试器分离。 Once I had done this, to actually beat the CLR using C++ I had to resort to low level techniques such as SSE/SSE2 and inline assembler. 一旦我完成了这个,实际上使用C ++击败CLR我不得不采用低级技术,如SSE / SSE2和内联汇编程序。

Ok, I'll admit, C# and managed languages are no match for hand-optimized C++ code in experienced hands, but I've seen poorly optimized code on both platforms. 好吧,我承认,C#和托管语言与经验丰富的手工优化的C ++代码不相匹配,但我看到两个平台上的代码都没有得到优化。 It's easy to blame the CLR when C# code is slow but when developers are using the new operator at will I find it odd they get surprised when the GC is run so frequently. 当C#代码很慢时,很容易责怪CLR,但是当开发人员随意使用new运算符时,我发现奇怪的是,当GC频繁运行时,他们会感到惊讶。 new and delete in C++ also imposes a performance hit so don't expect C++ compilation to just make things faster. C ++中的newdelete也会带来性能损失,所以不要指望C ++编译能让事情变得更快。

Back to your specific engine - you will have to of course do some testing and performance analysis yourself. 回到您的特定引擎 - 您当然必须自己进行一些测试和性能分析。 Regarding platform invoke, it does incur a performance hit known as thunking when pointers and structs are marshalled across the managed/unmanaged boundary. 关于平台调用,当指针和结构在托管/非托管边界上编组时,它确实会导致称为thunking的性能损失。 Pure managed code will not have this but also it will miss out on optimisations such as low level memory copy, SSE/SSE2 extensions etc... that can be coded in C++. 纯托管代码不会有这个,但它也会错过优化,例如低级内存拷贝,SSE / SSE2扩展等......可以用C ++编码。

Finally I will say that for an example of managed->Platform invoke library that is extremely powerful and fast, take a look at SlimDX . 最后,我将说一个非常强大和快速的托管 - >平台调用库的示例,请看一下SlimDX Ok you're going to get a performance hit over native code and DirectX (some sources say ~5%) but for the productivity benefits of developing in C# I'd say its worth it! 好吧,你的性能会超过本机代码和DirectX(一些消息来源说~5%),但是为了提高C#开发的生产效率,我觉得它值得!

Best regards, 最好的祝福,

but what i want to know is how a fully Managed Physic Engine is compared to a P/Invoked One ( For example tao.ODE ) in Windows Environment ( in term of performance ) ? 但我想知道的是如何将完全托管的物理引擎与Windows环境中的P / Invoked One(例如tao.ODE)进行比较(在性能方面)?

Both suck - the only way to get realy high performance these days is not "unmanaged" as in "processor code" but "unmanaged" as in "running on the graphics card". 两者都很糟糕 - 这些天获得真正高性能的唯一方法不是“处理器代码”中的“非托管”,而是“运行在显卡上”的“非托管”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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