Functions and High CPU Usage

Discussion and feedback on Construct 2

Post » Mon Jun 30, 2014 7:51 pm

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?
(M.A.D. are my initials. I'm not the least bit angry, really!)
@madguy90
B
11
S
4
Posts: 41
Reputation: 1,671

Post » Tue Jul 01, 2014 11:38 am

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.
Scirra Founder
B
395
S
231
G
88
Posts: 24,367
Reputation: 193,684

Post » Tue Jul 01, 2014 5:38 pm

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.
(M.A.D. are my initials. I'm not the least bit angry, really!)
@madguy90
B
11
S
4
Posts: 41
Reputation: 1,671

Post » Sun Nov 30, 2014 2:36 am

Wow, big spike, i see it as well. Is there an explanation for this? I'm experiencing a similar problem myself. @Ashley
B
24
S
4
G
1
Posts: 15
Reputation: 2,151

Post » Sun Nov 30, 2014 2:59 am

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
Don't lose your work. Backup your game with Dropbox.
B
44
S
10
G
10
Posts: 1,106
Reputation: 9,202

Post » Sun Nov 30, 2014 12:12 pm

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.
Scirra Founder
B
395
S
231
G
88
Posts: 24,367
Reputation: 193,684


Return to Construct 2 General

Who is online

Users browsing this forum: purplemonkey and 8 guests