Understanding the Cost of Methods


One of the most important ways to ensure that you write applications with the best performance is to always know the cost of any method you call. Although 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.

Managed DirectX has 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 the cost of allocating a new unmanaged string type and copying the string data over.

Take the case where you are using High-Level Shader Language (HLSL) to render your scene, and numerous times throughout each frame, you switch the technique being used, such as the following:

 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 is to cache the handles returned:

 // 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 

You should always know what an underlying method costs; I cannot stress this point enough.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

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