The speed of Managed DirectX

It seems that at least once a week i’m answering questions directly regarding the performance of managed code, and Managed DirectX in particular.  One of the more common questions i hear is some paraphrase of “Is it as fast as unmanaged code?”.

Obviously in a general sense it isn’t.  Regardless of the quality of the Managed DirectX API, the fact remains that it still has to run through the same DirectX API that the unmanaged code does.  There is naturally going to be a slight overhead for this, but does it have a large negative impact on the majority of applications?  Of course not.  No one is suggesting that one of the top of the line polygon pushing game coming out today (say, Half Life 2 or Doom 3) should be written in Managed DirectX, but that doesn’t mean that there isn’t a whole slew of games that could be.  I’ll get more to that later.

I’m also asked quite a bit things along the lines of “Why is it so slow?”  Sometimes the person hasn’t even ran a managed application, they just assume it has to be.  Other times, they may have run numerous various ‘scenarios’ comparing against the unmanaged code (including running the SDK samples) and have found that in some instances there is large differences.

Like I’ve mentioned earlier in this blog, all of the samples in the SDK use the dreaded ‘DoEvents’ loop, which can artificially slow down the application due to allocations and the subsequent large amounts of colllections.  The fact that most of the samples run with similar frame rates as the unmanaged API is a testament to the speed of the API to begin with.

The reality is that for many of the developers out there today, they simply don’t know how to write well performing managed code.  This isn’t through any shortcoming of the developer, but rather the newness of the API, combined with not enough documentation on performance, and how to get the best out of the CLR. Luckily, this is changing, for example, see Rico Mariani’s blog (or his old blog). For the most part, we are all newbies in this area, but things will only get better.

It’s not at all dissimilar to the change from assembler to C++ code for games.  It all comes down to a simple question.  Do the benefits outweigh the negatives?  Are you willing to sacrifice a small bit of performance for the easier development of managed code?  The quicker time to market?  The greater security?  The easier debugging?

Like i said earlier, there are certain games today that aren’t good fits for having the main engine written in managed code, but there are plenty of titles that are.  The top 10 selling PC games a few weeks ago included two versions of the Sims, Zoo Tycoon (+ expansion), Age of Mythology, Backyard Basketball 2004, Uru: Ages beyond myst, any of which could have been written in managed code.

Anyone who’s take the time to write some code in one of the managed languages normally realizes the benefits pretty quickly.

XBox Live

Given my job in the DirectX team, it’s probably a safe bet that people already realize i would be what is called a ‘Gamer’.  I spend entirely too much money on video games, and depending on the games that are out there, entirely too much time playing them.  However, right now my ‘gaming’ time is being spent almost exclusively on two games.

Obviously, i’ve had XBox Live since it was first launched in November of 2002, but honestly for the almost the entire first year it was out, there was never anything i really wanted to play on it.  Sure, i played some Mech Assault, and was into Unreal Championship for a while, but nothing i would consider ‘great’.  That’s been changing recently though.

For example, Project Gotham Racing 2.  This is probably the best XBox Live game available today.  Not only does it have a great multiplayer experience online, even the single player missions are all ‘live-enabled’.  Your best scores are stored on xbox live, and you can compare yourself to other players in all of the single player missions.  Better yet, you can actually download the ‘ghost’ of other players who have played the missions to see how they’ve acheived these outrageous high scores.  And since you’re always signed into live while you’re playing (even in single player mode), any of your friends who are also online can invite you to play a multiplayer game.  Even playing in the multiplayer modes lets you get kudos tokens you can use to help yourself during the single player mode.  This game should be the model for all future live games.  It is the entire package.  The only downside (if you could call it that), is that it doesn’t support XSN, which i can understand considering it isn’t a ‘sport’.

Speaking of XSN though, the other game i’m really into nowadays is Links 2004.  Joining tournaments online, and competing, and watching my stats is addictive.  Personally, i’ve tried Tiger Woods game, and it just doesn’t match up.  Sure, you have a wider array of choices when creating your character, but the golf isn’t as realistic, you can’t play with teams, and you can’t play with more than two players.  Playing in tournaments is the absolute best.

This doesn’t even consider the other great XBox Live games out nowadays..  Rainbow Six 3, Counterstrike, Crimson Skies, Midnight Club 2, etc.. After being out a year, i’m now really starting to be impressed..