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!

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.