1
\$\begingroup\$

I've searched around the web for a while, but didn't manage to find anything about it.

Despite using Animator in several projects so far, I've never been in this exact situation, so I'm unsure what to do.

Does calling Animator.SetBool have any performance issue, so that I'd better call it only when its value has changed, or when its value doesn't change it's so quick that I can call it every frame?

Keep in mind that, due to the nature of this question, I don't care if it's slow when I change its value, I only care if it's slow when it's false and I call it to set it to false again (same thing for true ofc).

\$\endgroup\$
2
\$\begingroup\$

Those calls aren't free, but they are very fast and usually aren't a problem.

When in doubt, check the profiler for performance hotspots. Unless you're making thousands of these calls per frame, they're probably not a big time sink for your game.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

This question falls into the subject of Runtime Analysis.

Assignments are, by nature, constant time operations. From a performance standpoint, you generally don't need to worry about constant time operations.

Since you asked: Any primitive operation(assignment, logical evaluation, etc) are generally considered to take one flop from a runtime standpoint. Occasionally there may be more cycles for other overhead, but as a general rule they are considered to take one flop.

Lets compare pseudocode:

Option 1 (Every Frame): 
     SET variable = false;     (1 Flop)

Option 2 (Only if necessary): 
     IF(variable != false)     (1 Flop)
       SET variable = false;   (1 Flop)

So theoretically speaking, with boolean assignment, it's actually slower to try to avoid assignment, than to assign it each frame.

The method declaration for the C# call is :: public void SetBool(int id, bool value); :: the id lookup is an O(1) operation off the heap from a hashtable, and the value assignment is an O(1) assignment.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ If it was a simple assignment, sure, but I don't know how it handles this call internally. Maybe it just sets a bool, maybe it does also other expensive operations, that's why I asked. \$\endgroup\$ – o0'. Jul 22 '15 at 22:44
  • \$\begingroup\$ I think it does more than just set a bool... It takes in a string or int which references a bool... I would have an internal book that would keep track of when to call SetBool, instead of setting it every frame. \$\endgroup\$ – Savlon Jul 22 '15 at 23:18
  • \$\begingroup\$ While I appreciate the effort, you really misread the question, sorry. I'm not "setting a bool", I'm "calling the SetBool function". That's absolutely not the same thing. \$\endgroup\$ – o0'. Jul 24 '15 at 13:11
  • \$\begingroup\$ Under the hood it absolutely IS the same thing. The method declaration for the C# call is :: public void SetBool(int id, bool value); :: the id lookup is an O(1) operation off the heap from a hashtable, and the value assignment is an O(1) assignment. Everything I said before applies. \$\endgroup\$ – Stephan Aug 12 '15 at 20:22
  • 1
    \$\begingroup\$ @Stephan I voted this back up to 0, but please move your comment about the method declaration into the answer (near the top) so people don't keep assuming that you didn't consider that. :) \$\endgroup\$ – David Mason Mar 1 '16 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.