Functions and High CPU Usage

0 favourites
  • 6 posts
From the Asset Store
High Low is a casino game using luck and points accumulation.
  • I'm guessing this is a known issue, but I haven't been able to find anything about it on the forum history. It looks to me like simply calling a function is using a larger amount of CPU power than I would have expected.

    As an experiment today, I wrote a very simple condition that looks like this:

    Test Group

    For Each PlayfieldObject

    If Variable1 = 1

    -> Set Variable 2 = 1

    There are about 40 instances of the sprite PlayfieldObject. Looking at the profiler, Test Group is using a very small amount of CPU memory, like <1%. Not bad considering this is running every tick.

    Then, I rewrote the code to incorporate a function:

    Test Group

    For Each PlayfieldObject

    If Function.Call("testFunction) = 1

    -> Set Variable 2 = 1

    On function "testFunction"

    If Variable1 = 1

    -> Set return value to 1

    To my surprise, this caused the CPU usage for the group to spike past 20%! Same basic functionality, the only difference being that in the second version of my code, I've used a function as one of my conditions. (and just to note, placing the function as an Action causes the same CPU spike).

    The game I'm working on has a lot of stuff going on at once (lots of AI characters, pathfinding, etc.), and while I'm attempting to be as efficient as possible, this concerns me, as I have a lot of functions being called on a regular basis-- not necessarily every tick, but I have some pretty extensive AI code that runs 5 to 10 times per second for each nearby character, and utilizes a lot of functions.

    Ashley is this something you were aware of? If this isn't something that can be solved, it seems like this might be worth a mention in the Performance Tips article?

  • It should not that have that serious an impact on performance. Note the CPU measurement can be inaccurate since it's based on Javascript timers, so it could just be due to the unreliability of the numbers you're looking at. Either way, post a .capx demonstrating this so we can investigate further.

  • Sure, here you go:

    https://www.dropbox.com/s/beczig6sgpctmb2/FunctionTest.capx

    This capx includes the two versions of the code. The second version is commented out.

  • Wow, big spike, i see it as well. Is there an explanation for this? I'm experiencing a similar problem myself. Ashley

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • When exported, cpu usage drops to almost nothing (1-2%). Minification makes no difference.

    Task manager seems to indicate that the cpu is actually under strain during preview. Chrome, FF, Node all exhibit the same behavior (cpu use elevated ~10x over export).

    Debug/Normal preview makes a significant difference when under the Inspect tab (cpu about 2x higher than normal), no difference under the Profile tab.

    Odd...

    Here's my version of the file (added a spritefont cpu counter):

    https://www.dropbox.com/s/l3l35x8g2nfaz ... .capx?dl=0

  • It happens because in preview mode it tries to identify functions that are mis-typed and log to the browser error console if so. In this case it incorrectly identifies the function call as not having triggered any "On function" event (apparently due to the second false condition that prevents the event running) and so incorrectly logs to the browser console that it's a possible mistake. Since it ends up logging several hundred messages a second it burns a lot of CPU.

    Post this to the bugs forum and I'll take a look some time for the next beta.

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