I profiled my XNA game project today using the XNA Framework Remote Perf Monitor and discovered that I was generating about 8000 more manage objects per second than I was expecting to.
It turns out that this code was generating 7400 managed objects per second:
text = "FPS: " + fps.FPS.ToString(); text = "charPos: " + charPos.ToString(); text = "tLOD: " + tLOD.ToString(); text = "tTris: " + tTris.ToString(); text = "tBlocksDrawn: " + tBlocksDrawn.ToString(); text = "tBlocksCulled: " + tBlocksCulled.ToString(); text = "drawCodeTime: " + drawCodeTime.ToString();
A complicated optimization for code that isn’t even going to get compiled into the release build would be silly. The easiest fix here is to only periodically update the content of these strings (say, once per second, instead of once per frame.)
Also, some of the garbage generation could be reduced by separating the arrays into one array for the static strings “drawCodeTime: ” for example and then drawCodeTime.ToString() in a separate string. That would still leave thousands of objects per second created, sadly.
If you have a game that relies on many string conversions, your best bet is to use StringBuilder objects (which have predefined size) and replace subsections of those strings with the new string using the .Replace method that StringBuilders have built-in.