What's faster between a 'foreach' and a 'for' on a chain?

Discussions about Coding and Scripting
Post Reply
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

What's faster between a 'foreach' and a 'for' on a chain?

Post by PrinceOfFunky »

I need to iterate through actors of the same class, it's a class I made so I can make it to chain with actors of the same class.
I was wondering, what is faster between a 'foreach' on that class and a 'for' on a chain of that class?
I would say a 'foreach' since it's native but a chain looks more direct since there won't be needed to iterate through all the possible actors(and probably even objects) in the whole level.
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
sektor2111
Godlike
Posts: 6403
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by sektor2111 »

Already answered twice in forum which is not read, and this is the third time with the same thing - SPAM.

"For" works for chained lists which under let's say 500 value (let's consider Pawns) works pretty nice. If Level has a crap-ton of pawns 2000+ "For" cycles goes slower than foreach. The fact is "for" uses UScript which is not fast and foreach even is checking a wide range of actors is still C++ in front of UScript.

Keep spamming then:
USE XC_CORE as said 100 times for clocking your scripts and put this stupid post in a memo because I'm not gonna reply another time the same thing.
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by PrinceOfFunky »

sektor2111 wrote:Already answered twice in forum which is not read
I'm not sure, I get one page only when searching "foreach for chain" and none of the listed posts talk about what's faster, I cannot read the whole forum by myself.
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
sektor2111
Godlike
Posts: 6403
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by sektor2111 »

Did you search for "XC_Core" "clocking" strings ? Of course forum cannot be read that easy because of useless crap posted all the time just to be in portal with posting, right ?

viewtopic.php?f=34&t=5624&p=78656&hilit ... ing#p78656
I was moving faster than this rig anyway...
nogardilaref
Masterful
Posts: 577
Joined: Tue Jun 20, 2017 1:00 pm
Personal rank: ⚋⚊⚌☰⚞⌖⚟☰⚌⚊⚋

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by nogardilaref »

I already benchmarked this heavily in the past on my own (I didn't use XC though, I used an UScript implementation), and the conclusion is pretty much what Sektor mentioned, on average.

It actually depends on 2 factors alone: the number of actors in a chained list, and the number of actors overall in the entire level at any given time.
Even with 2000 actors in a chained list, if you have something ridiculous like 300,000 actors overall in the level, the chained list is still far more efficient.

So the efficiency/speed here comes down to the number of actors in the list relative the number of actors overall, and this is because the native C++ code is many times faster and more efficient than UScript (about 20x if I am not mistaken, which is a whole order of magnitude doubled).

So although theoretically a chained list is always faster than iterating through all actors, in practice this stops being true when the ratio between actors in the list vs overall gets closer and closer to 1:1 (at around 1:20 I would say they would be even, due to the 20x speed difference on average, although it's probably not very accurate to say still due to the number of instructions performed, which is different).
And there's also the binding to consider: while the process is purely C++ side (filtering out unmatched actors), it's stupidly fast to perform, however the moment if finds one which meets the criteria, it has to return it back to UScript level and run it.
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by JackGriffin »

sektor2111 wrote:forum cannot be read that easy because of useless crap posted all the time just to be in portal with posting, right ?
I get your point Nels but you head down a bad road when you start saying things like that. I don't expect to find everything posted to be interesting but the staff does do a pretty good job at segmenting threads into a proper topic forum.

For instance MMU's 'fan art' is something I personally have zero interest in but others have found it to be worth a reply and small discussion. I've posted things with a single reply given lots of times. Does that make my posts useless too? I'd like to think not, I'm pretty careful not to spam crap.

This is the last useful and stable UT99 forum running so I'd rather people feel welcome to post, even if they are saying nothing worthwhile to you or me. Please don't paint with such a broad brush in an attempt to quiet postings you personally don't find 'worthy'.
So long, and thanks for all the fish
User avatar
sektor2111
Godlike
Posts: 6403
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by sektor2111 »

I did not post on offending purpose, it was just a simple reminder about forum efficiency.
While Higor, me and others were chating about interesting things, during this time I could figure some stupid posting even daring to say "I'm on forum portal right now". Yeah, was important portal rather than getting busy with discussions which were pretty much constructive for every coder regarding to skill level. I was privately informed that interest has been decreased about forum as long as some sudden addicted members were spaming dumb posts and information is buried and shadowed and heavy to find as long it was was covered with useless new "posts". But for me is not a loss, I saved those things in my head and into some small checking tool which I wrote and considering a more accurate clocking from XC_Core everything can be checked without actually making other noise.

Discussion about Sanity Checkers - It's way more nasty and spendy to access unexistent stuff and null values than a simply checker preventing all null useless access.
bool checking takes almost 0 time - it is faster than everything.
If (X != None) is a small quantity 0.000001 seconds on a rig at 1100 MHz or randomly almost 000.- probably logging result was more spendy here.
ISA - a little spendy but is helpful in preventing dependencies during replacements - is not that slow after all.
VisibleCollidingActors - various results - Ignore wiki here - if map uses big actors this is very expensive I would not use it that much in big radiuses.
RadiusActors - slow or fast depending on range - high ranges are even badly lagging game
Chained lists around 64 actors - fast enough to be used as iterator replacement - common matches with easy load.
FindPath... - hard stuff but it won't blow up the Earth... unless you wanna piss off 400 Monsters and then do not do what I did...
I did not dare to clock string checks - MapVoteLA are samples how to lag a server, like those AKALogxK things. Saving such an array takes a while - you have the chance to fall asleep after a harder day while that thing reads and saves data.

For refresh
viewtopic.php?f=34&t=5624&p=74156&hilit ... ter#p74156
Higor
Godlike
Posts: 1866
Joined: Sun Mar 04, 2012 6:47 pm

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by Higor »

How many do you intend to spawn?
Do they have a unique feature that makes them easily identifiable by it's properties (not actor reference, just a subset of properties)
Do you intend to add 'optional' XC_Engine code?
Are these linked to some main system?
How many hits do you intend to have on a single loop? (one, a %, all?)
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by PrinceOfFunky »

Higor wrote:How many do you intend to spawn?
Do they have a unique feature that makes them easily identifiable by it's properties (not actor reference, just a subset of properties)
Do you intend to add 'optional' XC_Engine code?
Are these linked to some main system?
How many hits do you intend to have on a single loop? (one, a %, all?)
1. (I can make them Object instead of Actor if it's faster) The user will choose how many, I think they will generally be few, probably less than 50.
3. Mhm no I don't think so.
4. Should I interpret this question as a foreach loop doesn't have to go from the first object to the last one basing on a sort? Anyway I probably didn't get what you meant, do you mean 4.1 if they are linked to a class or 4.2 are in the same package or 4.3 you're just asking if I can modify their code?
In case you meaent one of the sentences above:
4.1 They are all of the same class but they are not references in any, there is just one global variable that points to one of them(which extends that class).
4.2 Yes.
4.3 Yes.
5. Still, I'm not sure what you mean, this question sounds like the first one to me.
Last edited by PrinceOfFunky on Sat Nov 18, 2017 7:07 am, edited 1 time in total.
"Your stuff is known to be buggy and unfinished/not properly tested"
Higor
Godlike
Posts: 1866
Joined: Sun Mar 04, 2012 6:47 pm

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by Higor »

With optional XC_Engine code you can turn an AllActors loop into DynamicActors.
- Maximizes gain if many actors need to be hit. >>> native loops go down by 'n' amount of static actors (good if actors are spawned at start)

With a unique feature you can turn a For(chain) into a For(hash).
- Maximizes gain if one actor needs to be hit. >>> search loops go from [1-n] to [1-n/32], if actors can properly be encoded into a 'number'

Although only as many as 3 ppl on this forum can code a hashbin, good luck with that.
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: What's faster between a 'foreach' and a 'for' on a chain

Post by PrinceOfFunky »

Higor wrote:Do they have a unique feature that makes them easily identifiable by it's properties (not actor reference, just a subset of properties)
Sorry, I didn't see this question before, so numbers where messed up, I will renumber the answers.

EDIT: As for that question, they only have 1 function, they are all of the same class.
"Your stuff is known to be buggy and unfinished/not properly tested"
Post Reply