简体   繁体   中英

Managed Vs Unmanaged Physics Engines for C#

anyone tried BEPU Physic Engine ? 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.

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 ) ?

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?

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

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.

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

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. 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. new and delete in C++ also imposes a performance hit so don't expect C++ compilation to just make things faster.

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

Finally I will say that for an example of managed->Platform invoke library that is extremely powerful and fast, take a look at 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!

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 ) ?

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

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