High CPU Usage

Get help using Construct 2

Post » Mon Mar 06, 2017 7:01 am

Hi

So I made something today, however it has high CPU usage. Here is the source file:
https://www.dropbox.com/s/ap2hxpqlqoic2 ... .capx?dl=0

(The code is messy and is not well written)
Is there a more efficient way to do what i was trying to achieve?
B
45
S
19
G
3
Posts: 102
Reputation: 4,598

Post » Mon Mar 06, 2017 10:36 am

First, if you have performance problems. Put the events in groups, the less events in one group the better. Then check the CPU-Usage in the Profile-Tab in the Debug-Layout
Your performance problem is the function call. You're calling the function from a loop and in the function there is another loop. You have up to 250 sprites and for each sprite you call the function, which is going again trough all sprites. That's an a amount of up to 62500 loops in one tick! (1/60 sec).
I made a little change, don't know if that's still exactly what you want, but you will see I only check 2 sprites a tick, that will be call the function. So this are only 500 loops a tick.
The change in the function you can ignore, it was only a test and it seems you're gaining nothing from this change.
I hope I could explain this well enough, sorry for bad english.

https://drive.google.com/uc?export=down ... TdYaFM2aHc
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Mon Mar 06, 2017 10:55 am

Debug mode's profiler says that event sheet 1 is all of the high% CPU utilization here. That should give you a hint of where the CPU usage is being drawn from.

You have multiple culprits here, apparently, which are:
*Your "Repeat 100 times" loop
*Your A function
*Your For Each SpriteFamily event-chain

All of these are draining about 50-70% in increased CPU usage, so you may want to improve them or find ways of doing this without constantly polling Construct 2 repeatedly. (i.e, per tick stuff, repeat x times, etc)
Zelda Gamekit (my project)



Example capxs:

Zelda-Style Inventory

Zelda Health

3-saveslot system

[url=https://www.dropbox.com/s/d4396pwak0oh7q6/ZeldaDialogueSystem.capx?dl=0]Zelda-like Dialogue[
B
51
S
24
G
30
Posts: 534
Reputation: 19,620

Post » Mon Mar 06, 2017 4:51 pm

Yes i know what the problem is. The loop inside the function makes the problem and that is why because i couldnt find a better way to compare the distance of each instance of a same sprite with other instances. So the whole idea is to create a line sprite if 2 instances are closer than 50 pixels with each other. So is there any better way to check the distance of each two instances?
B
45
S
19
G
3
Posts: 102
Reputation: 4,598

Post » Mon Mar 06, 2017 5:23 pm

Asmodean wrote:First, if you have performance problems. Put the events in groups, the less events in one group the better. Then check the CPU-Usage in the Profile-Tab in the Debug-Layout
Your performance problem is the function call. You're calling the function from a loop and in the function there is another loop. You have up to 250 sprites and for each sprite you call the function, which is going again trough all sprites. That's an a amount of up to 62500 loops in one tick! (1/60 sec).
I made a little change, don't know if that's still exactly what you want, but you will see I only check 2 sprites a tick, that will be call the function. So this are only 500 loops a tick.
The change in the function you can ignore, it was only a test and it seems you're gaining nothing from this change.
I hope I could explain this well enough, sorry for bad english.

https://drive.google.com/uc?export=down ... TdYaFM2aHc

Your code works but it has a problem. A Square can only connect to one square at a time, but I want squares to connect to all the squares that are in the distance of 50 or less pixels
B
45
S
19
G
3
Posts: 102
Reputation: 4,598

Post » Mon Mar 06, 2017 5:38 pm

@HessamoddinS

That was only an example that you have to split up your nested loops.
You have to look how many sprites you have to check every tick. You can do this with a repeat loop:
Image

You have to try how many loops you need. The 10 is only a guess.
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Mon Mar 06, 2017 5:43 pm

@Asmodean ok thanks i will look into it
B
45
S
19
G
3
Posts: 102
Reputation: 4,598

Post » Mon Mar 06, 2017 10:34 pm

@HessamoddinS

You got me curious, so I tried a completely different approach...
instead of comparing every sprite to every other sprite, I used a larger sprite "detector" to find sprites within 50px of the active sprite.
plus I found a number of other ways to reduce the work it has to do every tick:

- only look for close sprites 4 times a second
- simplified the dictionary look-up
- only calculate distance once per line per tick
- don't update the text object every tick

now it hits 60 fps, and averages around 15 to 20% cpu

you can take a look at my code here: http://www.rieperts.com/games/forum/DotEffect.capx

Then I though it would be fun to make one sprite "sick" and see how long it takes to spread. So, press x on the keyboard to turn a random sprite red, then any time it connects to another sprite, that one gets infected and turns red too. (it doesn't take very long!)

press the space bar to turn them all back to black. (And then press x again to start another wave of zombie sprites)
B
97
S
34
G
20
Posts: 438
Reputation: 17,790

Post » Mon Mar 06, 2017 11:54 pm

@AllanR Thanks that was really helpful, I learned a lot
The thing is I was trying to implement this effect in the background to the game I'm working on and I had plan to use a lot more than 100 sprites on the screen, which i think would not be possible. I just hope there was a more efficient way that could handle around 300 objects.
B
45
S
19
G
3
Posts: 102
Reputation: 4,598

Post » Tue Mar 07, 2017 12:04 am

@HessamoddinS

I beleive that this is what you want to do.

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

Runs like a slow poke in the debugger.
Runs at 118 FPS on my laptop, outside the debugger.

I like the idea of using a dictionary to keep the amount of lines as low as possible.
But, then you gots to use the the dictonary's feature to not allow the same key twice. Look at how i solved that.
Have fun.

Edit: @AllanR, did not see your last capx yet. Had this topic open while coding, so topic was not refreshed when posting this¨.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 49 guests