Condition and function performance

1 favourites
  • 3 posts
From the Asset Store
Firebase: Analytics, Dynamic Links, Remote Config, Performance, Crashlytics on Android, iOS & Web Browser
  • Here's a small demo of the different ways to write conditions and their performance:

    so like, uh, a good rule of thumb is to structure your project to run with ternary operators inside of actions instead of using conditions, but that's only valid if you don't use the same expression evaluation like a billion times. If you check the same variable on multiple actions with ternary operators, you're better off putting it in a condition block so it skips all those instances in an action block

    You can use the ternary operator to close actions early if the expression isn't met, so it doesn't have to evaluate anything else, and end with "set self.var to self.var"

    note that a big factor in performance is what type of variable you use:

    here's a demo that checks how much a variable costs:

    note that the values don't start at 0%, there is a cost for the "Add to Variable" action, so i added "Test" to serve as a baseline "Cost" and so the costs of using variables by putting the cheapest one as a baseline is

    according to some math I made up with a single sample size so this is super inaccurate and is just a baseline to give a general idea that using "For Each" will costs 5x the CPU time

    Knowing all of this you can try and structure your code to use low cost variables when possible, like:

    Local and global variables for things such as "speed" to apply to your characters every tick

    when local and global variables aren't possible, use instance variables.

    When that type of data can't be stored in an instance variable store them in a dictionary or json or array, depending on the type and what you do with it, and don't use for each unless you absolutely have to.

    Note, it's more complicated than that, but takes time to get used to. Like, a dictionary might seem expensive at first glance but if you can use it instead of adding multiple conditions or a function, you're good!

    Also, using functions might seem prohibitively expensive, but if you can put a ton of conditions inside a single action that may allow you to not even call the function unless certain conditions are met; and call a function inside the same expression instead of doing a full "For each" so you can call a function as an expression instead of adding an action, which means you don't need to use conditions at all!

    Anyways, I hope that's interesting, uh, always remember to test test test, something might look better on paper but ends up being slower because of the javascript engine or whatever,

    also if this is too complicated for you, don't worry about it, it's not important, you can still make super cool games without any of this !!!

    Me please, if anything in here doesn't make sense, ask questions, there's no harm in asking questions, making mistakes and becoming cooler is cool, because like, what I'm writing makes sense to me but maybe not to you ? and it would be great if I could help you understand and stuff.

    I just spent like 2 or 3 hours writing this post so I'll end it here


  • This is very helpful, thank you :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you for your work, Eleanor! It's really helpful and insightful to see this with actual numbers.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)