1

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 1
    that sort of comparison is hard since it solely depends on what exactly you use the respective engine for... there is no general answer.., only way to find out for you is to code your use cases running on both engines... – Yahia Jan 21 '12 at 14:49
  • It really depends on the amount of P/Invoke calls you're doing and the amount of work each call is doing. – user703016 Jan 21 '12 at 16:58

2 Answers2

5

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!

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
1

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

TomTom
  • 61,059
  • 10
  • 88
  • 148