- Home /
Will caching strings improve performance for animation? (and other string calls)
Hello, I was wondering if caching strings in the Awake function will increase performance or would it take the same amount of time to call - specifically for calling animations.
Ex:
var walk : AnimationState;
var walkAnim : String;
var _animation : Animation;
var thisTransform : Transform;
function Awake(){
thisTransform = transform;
_animation = thisTransform.animation;
walkAnim = "walk";
walk = _animation[walkAnim];
}
function Update(){
_animation.Play(walkAnim);
}
Basically I'm trying to make my code as optimized as possible - this is just an example. I know strings are expensive to process so I was wondering if caching it this way was any help or if walkAnim will just refer back to "walk" as if the same as using "walk" in the first place.
Also, I use InvokeRepeating(); a lot which is formatted InvokeRepeating("someFunction", float, float) so would caching the name of the function help? I'm not sure how to go about testing these very well because I'm still saving up for Pro so no profiler.
Thanks!
Answer by sanderman0 · Sep 22, 2012 at 11:25 AM
Strings have to be compared and stuff to find the right AnimationState in the dictionary, so yes, caching that reference to the AnimationState will likely be more efficient.
In practice the difference should matter very little. Did you profile to see what sections of code need optimizing? It seems to me that this caching isn't really needed here. Wouldn't it be better to not call `Animation.Play()` every frame but instead to only call it when the animation should actually change? That way you only call it when the character stops moving or otherwise changes direction, which should be once per second or even less. I believe you would win much more performance with that than this caching I see happening everywhere.
Always remember: Premature optimization is the root of all evil. Also think before optimizing, often it's better to call a heavy operation less often than simply trying to make the thing more efficient.
Thank you for your answer. The example above is merely an example - I don't call animation.Play every frame but merely used it as an example of looking up a string very often - however you did re$$anonymous$$d me to check certain crossfades that are unnecessarily called more than once (they are looping already). Unfortunately I don't have the profiler (basic only). I have everything already functioning and after optimizing graphics I wanted to optimize the code ( i have a lot of different animation calls) but there is still some slow down - my guess is the enemies' textures are to blame but i already made every object use one texture atlas/material (with alpha) so I'm scraping around for performance.
///EDIT: Hey Sanderman0 it would be quite nifty if you could take a look at another question I posted here. It works off this topic but is different enough I thought it would be more helpful to others to create a new AS$$anonymous$$
http://answers.unity3d.com/questions/322134/more-animation-optimzation-using-booleans-to-check.html
Your answer
Follow this Question
Related Questions
Problem with makeing text appear on screen 0 Answers
Playing animation by String 3 Answers
Adding animation clips via script 2 Answers
Flash raw animation data to Unity 0 Answers
Optimizing OnGUI 1 Answer