Understanding the Cost of Methods

One of the most important aspects of ensuring that you write applications with the best performance is to always know the cost of any method you call. While memorizing all the different permutations of APIs you can call and the performance they take is unreasonable, you should have at least a vague idea about the cost of any method.

In Managed DirectX there are a few cases where seemingly harmless code can cause problems. For example, any method that takes a string as an argument in Managed DirectX has a cost of allocating a new unmanaged string type and copying the string data over.

Take the case where you are using HLSL to render your scene, and numerous times throughout each frame, you switch the technique being used, such as

 myEffect.Technique = "TransformWorldSpace"; //Render some stuff myEffect.Technique = "TransformAndAddGlow"; // Render some other stuff 

Each time you switch the technique, you are allocating some memory and copying the technique name over. A much better solution to this problem would be to cache the handles returned, such as

 // On device creations, cache technique handles EffectHandle handle1 = myEffect.GetTechnique("TransformWorldSpace"); EffectHandle handle2 = myEffect.GetTechnique("TransformAndAddGlow"); // Later on, for every render myEffect.Technique = handle1; //Render some stuff myEffect.Technique = handle2; //Render some stuff 

Another common case of not understanding the cost of methods was covered earlier when dealing with resources. The difference between locking a resource and returning an Array or a GraphicsStream is quite drastic.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net