Posts Tagged ‘xbox’

Speaking of Efficiency

Saturday, January 31st, 2009

When dealing with a real-time application, like say a video game, efficiency is key.  My last post pointed to some great articles about XNA efficiency, but there just isn’t any substitute for experience.  In my latest quandaries with XNA, I’ve been dealing with the C# generic data structures.  Here’s what I’ve found:

Hello Mr. Vector, meet Mr. List
If you’re familiar with C++ vectors, then a C# List is where you’d like to go.  A list is implemented as a dynamic array, so you may access each element just like you would with an array.  That comes in handy since we like to avoid those nasty foreach loops.  For the most part, the list implementation is fairly quick.  What isn’t quick?  

List.Remove(Object objectVariable)

Yeah, my ImagineCup team makes use of this for moving objects in the game.  The remove call seems to be the slowest we have, and it makes since.  To remove an item from the list, you must check the item against every item in the list or at least until you find a match.  Since list’s are unordered beasts, this is, at worst, O(n).  Do that a couple of thousand times and see if your XBox lags a bit.

Dictionary: It’s like a really slow hash
A dictionary is a new (at least to me) implementation in C#.  They are based on a key, value pair which is where the hashing comes in.  Given a key, the dictionary invokes a hash function to find the element you requested.  In my case, we used enumerations for the key, and a list for values.  This helped us keep organized, find things quickly, and all was well.  That is, until we realized how long it takes to actually grab items out of the dictionary.  Ordinarily, reaching values given a key will take a hash function O(1) time.  That’s really quick!  With a dictionary, you still find items in O(1) time, but the overhead to grab that item’s value is massive (perhaps an overstatement).  If you iterate through items (like you would using a list) this isn’t the data type for you.  For simple classification though, you might get by with it.

While trying to figure out things on my own, and make our game as efficient as possible, I stumbled upon this article at XNA Creators Club: Data Structures.  It covers (a little more in depth) how each structure works and it’s relative time complexity in big O notation.  It’s a good read if you’re having slow down in your game.  You might just be using a structure that’s killing you.

XNA Efficiency

Sunday, January 25th, 2009

In my brief (somewhat) experience with XNA, I’ve come to realize the XBox isn’t as all powerful as I had once assumed. True, it has a triple-core processor. Yes, it has a decent graphics card. All powerful, hardly, nor should it have to be.

As developers, we should always consider our need for power. A simple game might get away with quick and dirty algorithms. A more complex game, will involve more strategic design methodologies to get the most of out the XBox hardware. Overall, it becomes a bartering system with yourself. Tradeoffs in accuracy to increase performance.

I recently ran into some problems with my team’s upcoming submission to the Imagine Cup.  Luckily the XNA community is vast, so I’ve found out a few simple items: pass matrices by reference whenever possible, and try to avoid using foreach loops for large objects or things that will iterate a large number of times.  Great!  Fine!  What happens when it isn’t enough?  Thats when you have to start diving into the code to find the root of the problem.  Tom Aylesworth wrote a great article about just this.  He said we need to use profiling applications to get a grasp on what is actually (not what we think) is going on with our applications.  Profiling has been an invaluable tool in the XNA development process.

Tom suggests using Nprof since it is free and seems to work well (from the screenshots).  Unfortunately, I can’t get NProf working on my Windows 7 Beta machine.  I know, don’t use a beta for development.  It’s too risky, daring, unstable.  I get it, but I ignore those threats anyway.  Right now I’m running a trial of dotTrace.  Any suggestions on a free Win7/Vista alternative?

All in all, I strongly recommend reading Tom’s series about XNA efficiency especially if you’re new to C#.  It’s a three-parter:
Part1
Part2
Part3