Unity Community


Results 1 to 8 of 8

  1. Location
    Paris
    Posts
    3,730

    Caching AnimationStates for runtime calls is 10 times faster

    Hello,

    I only saw this tip in one old topic,and wasn't very sure about how efficient it was, so I ran a test :

    test 1 : assigning to a float variable the weight of an animation by calling it via this[] (ex : anim["walk"]), looping 10000 times


    test 2 : assigning to a float variable the weight of an animation by calling it via an AnimationState cache (ex : AnimationState ac = anim["walk"] before running the test, then calling ac.weight during test), looping 10000 times


    result :

    test 1 returned a duration of 50 to 80 ms per batch of 10000 calls
    test 2 returned a duration of 3 to 8 ms per batch of 10000 calls
    (tested on an i7 920 intel proc)

    Caching which animation clips you're calling is therefore 10 times faster.
    I didn't think it would be this huge a difference, tbh, hence the creation of this topic for further reference.

  2. Super Moderator
    Location
    Great Britain
    Posts
    9,666
    thanks for sharing man, that is very cool.

    Tell me about the game, is she progressing well?
    Currently working with Sony on our new
    PS4 and Vita game in Unity!

    This post is not representative of Simian Squared Ltd


  3. Location
    Paris
    Posts
    3,730
    Quote Originally Posted by hippocoder View Post
    thanks for sharing man, that is very cool.

    Tell me about the game, is she progressing well?
    Hello Hippo
    Yes, the lady has actually finished her diner, and is dressing up to party hard !
    She's putting a lot of attention into drawing those fine eyelines


  4. Location
    Paris
    Posts
    3,730
    I made another interesting test :

    It seems that storing all AnimationStates inside a Dictionary gives twice faster access than with this[animName], too.

    test 1 :

    Code:  
    1. float test;
    2.        
    3.         for (int i = 0; i < 10000; i++)
    4.         foreach(AnimationState ac in _anim)
    5.             test = ac.weight;


    test 2 :

    Code:  
    1.         for (int i = 0; i < 10000; i++)
    2.         foreach(KeyValuePair<string, AnimationState> kvp in lib)
    3.             test = kvp.Value.weight;



    There are 510 animation clips inside the Animation sampled component.

    test time with this[clip] : 320 ms
    test time with Dictionary : 151 ms

    The only cullprit is the Dictionary generation time, which was in this case : 231 ms.

    So it seems like using this[animName] is absolutely not recommended at all.

    edit : Additionally, storing the 510 x 2 animations (2 characters) inside the Dictionary didn't create any Memory overhead at all. So I guess that it's really a benefit to store any anim clip, even if it costs 2 seconds more at startup.

    p.s : I measured the base difference between ms values on my PC and on a Galaxy S, and you can expect 10 times the duration of PC test results on the Galaxy S. So I guess 15-20 times on a 3GS, 5 times on a 4S.
    Last edited by n0mad; 03-03-2012 at 10:42 AM.


  5. Location
    Chicago
    Posts
    859
    Hey n0mad! Might I ask two things of you for complete clarity?

    1.Can you link the original tip-thread you mention? I believe it was Mika's thread(regarding Zombieville) but I'd like to see the one you're referencing.

    2. Can you post pseduo-code for the two original tests you started this thread with?

    Thanks man!

    -Steve
    Steve Bodnar - Animation Director
    FlipFrame Animation/Games

    Recent Project - Bioshock Infinite
    Current Project - 'Unreleased'
    -------------------------------------------------
    Unity3D Pro / iOS Pro


  6. Location
    Paris
    Posts
    3,730
    Quote Originally Posted by SteveB View Post
    Hey n0mad! Might I ask two things of you for complete clarity?

    1.Can you link the original tip-thread you mention? I believe it was Mika's thread(regarding Zombieville) but I'd like to see the one you're referencing.

    2. Can you post pseduo-code for the two original tests you started this thread with?

    Thanks man!

    -Steve
    Hi Steve

    1 - I found this thread last week, so I didn't save the url, sorry :/ But yeah, I recall Mika asking similar questions for Zombieville ^^

    2 - sure :

    test 1 (this[myAnimName]) :

    Code:  
    1. float test;
    2. for (int i = 0; i < 10000; i++)
    3.  test = myCachedAnimationComponent["walk"].weight;

    test 2 (cached AnimationState) :

    Code:  
    1. float test;
    2. AnimationState ac = myCachedAnimationComponent["walk"];
    3. for (int i = 0; i < 10000; i++)
    4.  test = ac.weight;


  7. Location
    Chicago
    Posts
    859
    Awesome!

    Also, just in case this is what you were looking at:

    http://forum.unity3d.com/threads/207...iles-of-memory

    Years ago, yet it's one area inexperienced developers overlook. Significant if I may say so...

    Thanks n0mad

    -Steve
    Steve Bodnar - Animation Director
    FlipFrame Animation/Games

    Recent Project - Bioshock Infinite
    Current Project - 'Unreleased'
    -------------------------------------------------
    Unity3D Pro / iOS Pro


  8. Location
    Paris
    Posts
    3,730
    Ah yes, precisely

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •