[JASS] ForGroup or Loop?

Status
Not open for further replies.
Level 17
Joined
Feb 11, 2011
Messages
1,860
Hi guys,

I am wondering which is more efficient:

JASS:
call ForGroup(group whichGroup, code callback)
OR the method used in this example:
JASS:
function Mass_Sleep takes nothing returns nothing
    local group g
    local unit u
    local unit cast
    local unit dumb
    local location p
    
    set cast = GetTriggerUnit()
    set p = GetSpellTargetLoc()
    set g = GetUnitsInRangeOfLocAll(800.00, p)
    set u = FirstOfGroup(g)
    loop
        exitwhen u==null
        set u = FirstOfGroup(g)
        if  IsUnitEnemy(u, GetOwningPlayer(cast))==true then
            call GroupRemoveUnit(g,u)
            set dumb = CreateUnitAtLoc(GetOwningPlayer(cast), ‘h000’, GetUnitLoc(u), 0.00)
            call IssueTargetOrderBJ(dumb, “sleep”, u)
            call UnitApplyTimedLifeBJ (1.50, ‘BTLF’, dumb)
            set dumb = null
        endif
    endloop
    
    set g = null
    set u = null
    set cast = null
    set p = null
endfunction
 
Level 6
Joined
Jun 16, 2007
Messages
235
Loop is faster and slightly more efficient.
No it isn't.

This kind of beliefs come from stupid speed tests that have no relation to real mapmaking.

The truth is that the stuff you actually do with units in group is much more CPU intense. Compared to it loop or enum function overhead have as much effect as a flea on a horse.

I personally use both loops and enums.

Advice: Use what makes your code easier for you, and leave imaginary optimizations for noobs.
 
Both are efficient, but ForGroup is more dynamic, why?, coz you cant delay a loop...

ForGroup needs EnumUnit, Timer (or looping trigger) and another function to
loop on, whilst a Loop doesnt need except for the current function, so dont
listen to cohadar coz loops are faster...although FoG is a loop aso XD...he's
right at one point >>> Use what makes your code easier for you...
 
Level 5
Joined
Jan 4, 2009
Messages
118
really!!

I just know ForGroup is the best way to use spell group, but Why in many site is mention loop the best than Forgroup. Arr I confusing in two function...

Plz Tell me more I should select and use ForGroup or Loop ?
 
If you are determining which to use, it really depends. Both have their own benefits and uses.

ForGroup - starts a new thread (iirc) and will allow you to preserve the group members through the process. If you are using groups over time (as in, not just for a quick enumeration or something), then you will probably want to use ForGroup as it simplifies the preservation.

Loop - allows you to use local members of the function instead of having to assign global(s)/arrays to pass the data to the enum function. It will result in the group being empty in the end. (although, that doesn't serve much purpose since we have GroupClear() and enumeration's auto-clear) If you need a quick enumeration/grouping, this will often be the easier method to implement.
 
Speed wise, the loop method is faster. I've personally benched it myself. It is actually quite a lot faster than the ForGroup. The reason is primarily because of the executed code. Executed code does special things to allow the use of TriggerSleepAction. Evaluated code is quite a bit faster, but still slower than the loop.


This was benched when I was doing unit in range event, where I used TriggerRegisterUnitInRangeEvent rather than a group loop (surprisingly only slightly faster, telling me that the overhead on the first of group loop is rather minimal or that the TriggerRegisterUnitInRangeEvent sucks =p).
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
coz you cant delay a loop
You can if you use a trigger action or executefunc.
don't
listen to cohadar
No, I disagree, you should, instead of focusing on what's fastest, focus more on what really is important; in this situation, he stated that the actions done on units are in more need of optimizations (or attention) rather than that of the methods in question.
Remember, execution speed also depends on the method in implementation.

It is more efficient to perform your loop code in the filter that you supply to a group filler.
This, in my humble opinion, is the best method for spell scripting, as using ForGroups and FirstOfGroup loops produce unnecessary execution on dynamically enumerated groups, don't forget about the limit op though. Use the latter methods on static groups, as filtering of the said units occur only once which makes DSG's suggestion inapplicable.
 
Status
Not open for further replies.
Top