XNA performance
In the past i learned c++ cause its faster as managed code for games and stuff.
Now we are about to start a pretty large game project. This project will be an multiplayer first person shooter (swat kinda game, like Swat4 of sierra, Raven Shield of UbiSoft, ...). Cause high performance is a big requirement on this project we want to know if XNA framework (using XNA studio express) is the best (or good) sollution for this.
Writing this game in c++ will be a long, long, long way, maybe XNA can shorten this development process, only we want to be sure about the performance.
If you know what you're doing with managed languages, then it will be fast enough easily.
The truth is, that you will be hard pressed to find a specific area of coding that is faster in C++ than in C#. Calling methods/functions, performing primitive math operations etc is just as fast in C# as in C++, in fact memory/object allocation is even faster. The main reason that managed development is less performant actually has nothing to do with the language, and everything to do with the API.
Here is an example of something that will hurt performance in xna (yes, I'm mentioning this again):
public void Vector3.Transform(Matrix matrix);
The 64 byte matrix is being "passed by value" to the transform method. You would NEVER see code like this in a good C++ Api. Here is the corresponding method in Direct3D:
D3DXVECTOR4 * D3DXVec3Transform(D3DXVECTOR4 * pOut, CONST D3DXVECTOR3 * pV, CONST D3DXMATRIX * pM);
Although C# doesn't support "contant" pointers you can still pass structures by reference which has the same performance as passing by constant pointer.
So C# "the language" has the ability to be just as performant as C++, but API authors often hamstring it's performance by poorly performant design.
Other than those issues, which you can avoid, or "write your own", you are likely to be more performant in C# simply because the extra time you save using it can be used to perform algorithmic optimizations which are always much more significant than language differences.
Just take a look at the Marble Blast Ultra port to C#. According to the team, performance was virtually indistinguishable from the C++ version.
Case closed. 
hmm, k, seems like i'm gone use XNA :)
maybe i know why i had performance differences with managed and native code.
The managed code i used with the DX SDK was VB.NET 1.1.
Maybe there is just an performance difference between VB.NET and C#.NET and/or .NET framework 1.1 and 2.0
XNA makes the development of a game so much easy'er as programming from zero with the DX SDK.
I don't really care about small performance issue's. The game code i was writing in VB.NET 1.1 was a heighmap terrain using bezier patches and dynamic lod, heightmap colision detection, fustrum clipping on the tiles, so there was a lot of calculation in there having 10FPS. Rewriting the same code in c++ (without any c++ experence) resulted in a 100-150 FPS.
I won't worry about that... or yes I would, but the problem is not due to working managed. Although I'm not sure with VB.Net, but with C# if you do your code well you definately will not lose 90~95% of your performance. More like the other part; losing 5~10% in the worst case.
Of course it comes down to optimalizing your code, if you do your best at creating crappy code in C# you'll be able to ruin a perfectly running terrain engine as well down to like 10 FPS :).
Passing structs by reference is far worse than passing by value, because the CLR does boxing behind the scenes. It does not actually pass the pointer (unless they changed this in 2.0, which I doubt). Structs are best passed by value, or with pointers, but that has serious limitations in C#. In fact, if you are making structs to pass them by reference, it completely defeats the purpose of using value types. Nightmare_BE wrote: |
| hmm, k, seems like i'm gone use XNA :) maybe i know why i had performance differences with managed and native code. The managed code i used with the DX SDK was VB.NET 1.1. Maybe there is just an performance difference between VB.NET and C#.NET and/or .NET framework 1.1 and 2.0 XNA makes the development of a game so much easy'er as programming from zero with the DX SDK. I don't really care about small performance issue's. The game code i was writing in VB.NET 1.1 was a heighmap terrain using bezier patches and dynamic lod, heightmap colision detection, fustrum clipping on the tiles, so there was a lot of calculation in there having 10FPS. Rewriting the same code in c++ (without any c++ experence) resulted in a 100-150 FPS. |
|
Sounds like you made a mistake in writing the managed code. More important than the language itself is how you use the language and how well you know how to write efficiently in that language.
Wrong lecler. We are talking about using the 'ref' keyword NOT casting to a reference type. There is a big difference between the two, although the common use of the term reference causes lots of confusion.
For details on the ref keyword (which is equivalent to passing by address in C++) look here:
http://msdn2.microsoft.com/en-us/library/14akc2c7.aspx
The ref
keyword causes arguments to be passed by reference. The effect is that
any changes made to the parameter in the method will be reflected in
that variable when control passes back to the calling method. To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword. For example:
Jim Perry wrote: |
| Just take a look at the Marble Blast Ultra port to C#. According to the team, performance was virtually indistinguishable from the C++ version. Case closed.  |
|
Errm, almost but not quite... The XBox 360 CPU is implemented in a radically different way to a desktop processor, for instance is heavily reliant on instruction scheduling to avoid pipeline hazards and get anything like decent perfomance. We'll have to see if they have done all that in the first jitter, so we'll have to wait and see how well the 360 CLR runs before closing the case.
I read an article on a benchmark between Java and C#...
http://www.bentuser.com/article.aspx?ID=323&page=1
Java has the roughly the same ideas as C#, and was glofified on the same idea in making games more easier to write, but everyone knew at the time of how horrible Java was in performance. C# is a different story. I gotta admit, C# is looking rather tasty rather than the Java alternative in performance. Programming in C++ or C# are some nifty solutions, so XNA could be a good start. The only problem is there are no books on the subject, so it's pretty much you, good/horrible tutorials online strictly of hobbyists as of now, Microsoft's beta documentation, hardly any topics on performance knowledge rather than reading books on the CLR etc. Native code, you have paid professionals who have been using it for so long now that you're more than bound to get qualified information and books from them (as of now). Of course, this is going to change tomarrow or the next day if XNA manages a longer lifetime than managed directx. If you read books on 3d theory, 3d math, etc. I suppose you would be able to jump right into XNA with minor struggle. You could read native DirectX books, but the transition won't always be obvious, but I'm sure the same logic applies over.
redshock wrote: |
|
That benchmark doesn't seem to have the source code published, so cannot be verified and is meaningless. My experience of Java 5 is that it outperforms C# in many cases, and equals it in all the others - so I'd guess the benchmark authors didn't download the full JDK and enable the -server vm option that makes a lot of code run about 3x faster.
That said I think the C# language is far better suited to games programming.
And you're right, if you know 3D maths, how graphics cards work etc then XNA is like falling off a log ... so I guess thats a clue what to read up on right now!
AndyL do you have a link to the Java 5 vs C# benchmarks? I'd be interested in reading about that.
Keith Newton wrote: |
| AndyL do you have a link to the Java 5 vs C# benchmarks? I'd be interested in reading about that. |
|
Keith,
all of the benchmarks are my own and game specific (e.g. collision detection of non-convex meshes), but I also tested the C++, C# and Java performance of the Scimark2 numerical benchmark. Since then someone else has done the same thing, which should save you compiling it. My results were almost the same on Windows XP/Pentium 4, except the latest GCC performance is pretty close to that of Visual C++ 2005 with the right optimisations turned on.
Andy.