Nested loops huge perfomance decrease[SOLVED]
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.
Nested loops huge perfomance decrease[SOLVED]
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.
- Pentarctagon
- Project Manager
- Posts: 5564
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Nested loops huge perfomance decrease
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: Nested loops huge perfomance decrease
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.
- Pentarctagon
- Project Manager
- Posts: 5564
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Nested loops huge perfomance decrease
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: Nested loops huge perfomance decrease
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.
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.
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
Re: Nested loops huge perfomance decrease
If you suspect it is matter of [foreach] and [for] implementation, you can do it with [while].
- beetlenaut
- Developer
- Posts: 2825
- Joined: December 8th, 2007, 3:21 am
- Location: Washington State
- Contact:
Re: Nested loops huge perfomance decrease
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
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
Re: Nested loops huge perfomance decrease
I don't know how I missed find_inbeetlenaut wrote: ↑January 28th, 2023, 11:51 pmThis 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.
This works like a charm, no loops required, no lag whatsever. Thank you so much and thanks everyone else for help!