Nested loops huge perfomance decrease[SOLVED]

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
User avatar
Inkerrio
Posts: 38
Joined: September 1st, 2021, 8:09 pm

Nested loops huge perfomance decrease[SOLVED]

Post by Inkerrio »

Hello, Is it normal that nested loops (especially of the same type, [foreach] within [foreach] and [for] within [for]) hugely decrease perfomance, to the point of being pretty much unusuable, or am I doing something wrong?
Last edited by Inkerrio on January 30th, 2023, 7:34 am, edited 1 time in total.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Nested loops huge perfomance decrease

Post by Pentarctagon »

What is the size of each array being looped over?
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Inkerrio
Posts: 38
Joined: September 1st, 2021, 8:09 pm

Re: Nested loops huge perfomance decrease

Post by Inkerrio »

Pentarctagon wrote: January 27th, 2023, 11:05 pm What is the size of each array being looped over?
Dozens, potentially hundres of elements. I compare an array (area) of hexes to another array of hexes and determine if they have the same coordinates.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Nested loops huge perfomance decrease

Post by Pentarctagon »

It'd depend on what exactly you mean by "performance decrease", specifically. Generally though I wouldn't think that's too unexpected. Looping over an array of 250 elements means 250 loops, but looping over two arrays of 250 in a nested [for] or [foreach] is 62,500 loops (if for some reason you're comparing each coordinate against every other coordinate).
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: Nested loops huge perfomance decrease

Post by Helmet »

You might make a tiny 5x7 map and see if that speeds things up, then methodically increase the map size and see what that does.

You also might consider posting your suspicious code and let the experienced coders skim it. Maybe there's a way for the code to be made more efficient.

Good luck.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Ravana
Forum Moderator
Posts: 2948
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Nested loops huge perfomance decrease

Post by Ravana »

If you suspect it is matter of [foreach] and [for] implementation, you can do it with [while].
User avatar
beetlenaut
Developer
Posts: 2814
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Nested loops huge perfomance decrease

Post by beetlenaut »

Inkerrio wrote: January 28th, 2023, 1:30 pm I compare an array (area) of hexes to another array of hexes and determine if they have the same coordinates.
This is what find_in is for. Loop over one array and use find_in to see if the location is also in the second. You can also use a [while] to break out of the loop immediately if the find_in check ever fails.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Inkerrio
Posts: 38
Joined: September 1st, 2021, 8:09 pm

Re: Nested loops huge perfomance decrease

Post by Inkerrio »

beetlenaut wrote: January 28th, 2023, 11:51 pm
Inkerrio wrote: January 28th, 2023, 1:30 pm I compare an array (area) of hexes to another array of hexes and determine if they have the same coordinates.
This is what find_in is for. Loop over one array and use find_in to see if the location is also in the second. You can also use a [while] to break out of the loop immediately if the find_in check ever fails.
I don't know how I missed find_in

This works like a charm, no loops required, no lag whatsever. Thank you so much and thanks everyone else for help!
Post Reply