# Do a lot of similar calculations efficiently?

0 favourites
• 9 posts
• Say I want to calculate the value of a function f(x) for values 1 to 10000. I can easily use a FOR loop for that, but that appears to be very slow. Is there a way to do it more efficiently? Thanks for any suggestions.

• It very much depends on your function. Loops themselves are not slow. You can put some simple math formula in a "Repeat 10000 times" loop, execute it on every tick and still get 60 fps.

• Thanks. Does that mean "Repeat" is better than "For"?

• No, I'm pretty sure in terms of performance they are the same.

• what exactly is your logic? can you post a screenshot?

depending on the logic, some sort of caching might speed it up for you.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• I am trying to create an array to store the values of some curve. What I have is this

For loop in 1:10000

Set Array.At(loopindex) = f(loopindex)

f is some mathematical function.

This is done at the start of layout, and I notice a lag (around 0.5 second) switching between layout. The game then runs at 60 frames per second, but I am just wondering if I can do something smarter so that user can experience a smoother transition.

• I am trying to create an array to store the values of some curve. What I have is this

For loop in 1:10000

Set Array.At(loopindex) = f(loopindex)

f is some mathematical function.

This is done at the start of layout, and I notice a lag (around 0.5 second) switching between layout. The game then runs at 60 frames per second, but I am just wondering if I can do something smarter so that user can experience a smoother transition.

could you try some sort of wait screen or transition screen ?

• Yeah, since you are doing this on start of layout, you can mask this lag using some transition effect.

For example, put a big black TiledBackground on top, add Fade behavior, set it to fade out and destroy after 1 second, then change your loop to this:

``````for x=0 to 9     -> Wait 0.1*loopindex
for y=0 to 999  -> Set Array.At(loopindex("x")*1000+loopindex("y")) = f(loopindex("x")*1000+loopindex("y"))
[/code:12uzv96i]``````
• Thanks guys for the suggestion. Will try to implement it and see.

• 9 posts