I am writing a game using C# 2010 and XNA 4.0. I want it to be a good game and not some "just another crap" and so one of my goals is good framerate. For this I would like to ask you for some advices, be it XNA related or C# related - what can I do to speed up my code and so improve FPS.
Here is some stuff I found out:
array
insted of list<>
will improve performance a lot, in case you want to access huge amount of data (or even lots: having an array of 20000 items gave almost 180% FPS of list of 20000.for
instead of foreach
will improve performance. On same 20000 elements it's like 5-10% difference.drawuserprimitives
and not Model
class. (no idea if it is 4.0 only, or 3.1 had such performance problem too)Ok, so to make long story short, please post some good advice here, so I / we can make good, fast and optimized games ;)
Thanks in advance: Zéiksz
Software optimisation is a craft. The best kinds of optimisation are evidence based on collected statistics.
Until you have a problem don't optimise. Don't you think that game playability is more important than this metric or another? My suggestion is make the game first and then consider where it's not performing as you'd like.
Then post some specific questions and I'm sure you'll get some help here on the specific issues.
In the mean time I suggest that you'll get more information from games development books like those listed here : http://forums.create.msdn.com/forums/p/8642/45646.aspx.
XNA Extereme 101 - seems to be an interesting tutorial
My 2 cents worth:
Don't put all your code in one method because you think it costs to call a method. If you THINK your losing 1-3 FPS by calling an empty method, You probably haven't finished researching the 1-3 loss yet. For that loss to be true during an empty method call, you'd have to have a FrameRate in the MANY thousands.... where you wouldn't concern yourself about a 1-3 drop.
FPS is a bad way to judge performance in XNA. When you have variable time step enabled, other processes that the OS is involved with can influence your frame rate. You really need to profile instead. Just concentrate on the amount of time it actually takes to complete the Update &/or Draw Methods by timing them with a System.Diagnostic.Stopwatch object. time them individually, not together (very important).
The 'new' keyword should not only be avoided in the draw call but anywhere in both the update & draw... FOR reference types only. Use the new keyword anytime and anywhere you need to with value types.
UberGeekGames has a good article on XNA optimization. It's mostly targeted at Xbox and WP7, but many of the things also apply to PC.
http://www.sgtconker.com/2011/05/high-end-performance-optimizations-on-the-xbox-360-and-windows-phone-7/
Original site is dead, but is available in the Internet Archive:
Archived version of High end performance optimizations on the xbox 360 and windows phone7
The rule of thumb is "profile before optimizing".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With