Is it possible to "divide" a loop on several ticks ?

Get help using Construct 2

Post » Sat May 07, 2016 3:21 pm

Hello,

I got a project with an essential "For each" loop that can be CPU intensive. I have to restrain it to every x seconds action instead of every tick to be comfortably playable at some level in the game. I was wondering how it is possible to divide, or allocate this loop into several ones. For example, I would like to check the first half of the picked instances in a tick, and the second in the next one. Or if I use "every x seconds", check the first half at t 0, the second at t 0.15 seconds, again the first at 0.3, etc...

Any idea ? Did anyone ever come to this optimization ?
B
14
S
4
Posts: 112
Reputation: 1,287

Post » Sat May 07, 2016 4:47 pm

just an idea
If you would make a variable that you change every 0.15 you could make a condition to your loop to run it only when var is set.

  • System > On start of layout | system > load game from slot "brain"
  • System > On load failed | system > scroll to obj_Forum
B
17
S
8
G
5
Posts: 340
Reputation: 4,707

Post » Sat May 07, 2016 5:31 pm

Hey @fm4fanAT,

Thanks, that seems to be the way. But I just made a stress test and found no improvement, CPU usage is the same. I set an instance variable for the sprites and a global variable for keeping track of checked groups. On sprite creation, I set the instance variable to the global and increment the global (from 0 to 2, so I got 3 groups of sprites). In the loop I filter the selected sprite to check with condition (if instance variable match the actual global) and increment the global. Before I tested every 0.6 seconds, now 0.2 with 3 times less calculation per loop.

So it does not lower the total amount of calculations needed of course, but only better divide the amount during same time as before. It only decreases little stuttering that can occur when there is too much calculation in one big loop, by doing more smaller loops.

Very good in theory, but few results in practice.

Do you know other kind of optimization when it comes to large number of units ?
B
14
S
4
Posts: 112
Reputation: 1,287

Post » Sat May 07, 2016 5:37 pm

We'd have to see what you are doing.
ImageImageImage
B
71
S
22
G
248
Posts: 3,756
Reputation: 138,058

Post » Sat May 07, 2016 6:32 pm

you can do it with a normal for loop and the pick nth condition. Like the first half would be:
For "" from 0 to Sprite.count/2
Pick Sprite instance loopindex
B
94
S
33
G
113
Posts: 5,356
Reputation: 73,273

Post » Sat May 07, 2016 7:25 pm

@blackhornet, I could make a little capx but it's more about ideas on optimizations for many units.

Yes, thanks @R0J0hound. What I did and explained in my last post is very similar. But as I told it's very small improvement unless you have hundreds or thousands of units. It basically splits the amount of calculations, that remains the same, onto multiple smaller loops. Instead of having one periodic "big" stutter, you get small ones.

I thought it could be a great optimization, but it's "only" a good little trick. Am I wrong ?
B
14
S
4
Posts: 112
Reputation: 1,287

Post » Sat May 07, 2016 10:53 pm

B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat May 07, 2016 11:08 pm

Nabu,

Running a thousand loops will take just as long all running in one frame, or split 100 each into ten frames. The advantage of splitting them is that you can update a progress par for instance, since you get frames during the duration of the loop to draw updates.

That said, there are generally many ways to optimize loops to speed them up, either by reducing the number of loops, lowering the amount of data that needs to be checked by conditions, reducing the actions taken per loop, ect.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,697
Reputation: 64,442

Post » Sun May 08, 2016 12:03 am

Thanks @99Instances2Go, the plugin looks great. Hard to adapt an already existing project, but promising !
Also thank you @oosyrag for the advices.
B
14
S
4
Posts: 112
Reputation: 1,287


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 47 guests