HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

How much faster is ForGroupBJ than making an array and looping?

01-13-2007, 07:24 PM#1
CaptainPicard
I think Vexorian proved awhile ago that using ForGroupBJ to do some action to each individual unit in a group was faster than making an array out of the group and then looping over the array. However, I have a very nasty problem involving a ForGroupBJ function that seems it will require me to make lots of global variables (which was the point of going to JASS in the first place) because it involves a function being passed to ForGroupBJ that I can't seem to pass any variable arguments to.

My thought is that I should just keep everything within the same function by enumerating all members of the unit group in a unit array and looping.

Any objections?

Any suggestions?

Thanks!

Capt. Picard
01-13-2007, 07:27 PM#2
wyrmlord
ForGroupBJ just checks a few conditions and then runs ForGroup. I've never run into any problems with just looping for each unit in a group, and that's probably the easiest way I'd think.
01-13-2007, 07:45 PM#3
darkwulfv
Personally I like to use ForGroup. It works better for me than running a loop. Don't use ForGroupBJ, use ForGroup.
01-13-2007, 08:00 PM#4
CaptainPicard
But, how would I call ForGroup with a function that I can pass local variables to as arguments? I'm perfectly happy to skip the extra function call to ForGroupBJ, but I guess my problem is that I don't know what a "callback" is.

Thanks DarkWulfV!
01-13-2007, 08:14 PM#5
wyrmlord
You would have to use globals or gamecache in some way.
01-13-2007, 08:15 PM#6
SFilip
Well you don't really need to pass locals for a group callback - you can just redefine them in the callback function and there you have it...I don't see anything special you would need to have there, but do correct me if I'm wrong.
A timer callback or a trigger on the other hand needs to somehow keep track of local variables...
For this you can use the CSCache (a part of Vexorian's Caster System).
Example use:
Collapse JASS:
function TCallback takes nothing returns nothing
    local integer i = GetAttachedInt(GetExpiredTimer(), "i")
    call BJDebugMsg(I2S(i))
    call AttachInt(GetExpiredTimer(), "i", i + 1)
endfunction

function Main takes nothing returns nothing
    local timer t = CreateTimer()
    call AttachInt(t, "i", 0)
    call TimerStart(t, .1, true, function TCallback)
endfunction
Should be self-explanatory.
01-13-2007, 08:28 PM#7
darkwulfv
A group callback is the function that you intend on using on the units in the group. It contains the actions you wish the perform on the units in the group. If you want a quick tutorial, just make a unit-group multi-action action in GUI and convert to JASS. Follow the function name and you'll understand.

Use the Local Handle Vars system to carry your variables over. That's how I did it (thanks Rising_Dusk!).
01-13-2007, 08:28 PM#8
Captain Griffen
I'd have said array loops are faster. Array loops done properly are lightning fast, so long as you can add the units to the array and remove them efficiently. I'd be very suprised if ForGroup is faster. If it is, then really the difference is effectively nothing unless you are doing it a tens of thousands of times a second, and then the other operations you do would probably be the main problem.

If you are worried about speed, Local Handle Variables are about as slow as you can go.
01-13-2007, 09:04 PM#9
Rising_Dusk
I remember a topic about this a long time ago.
ForGroup is a hell of a lot faster than FirstOfGroup iterations, but only slightly faster than array-loop iterations.

Unless you're doing 10000 unit groups, the speed difference is not noticeable in-game.
01-13-2007, 11:34 PM#10
Vexorian
What I proved was that ForGroup was faster than FirstOfGroup iterations when you need to keep the group. And don't use ForGroupBJ as almost any other BJ function, it is useless, get used to ForGroup
01-15-2007, 11:43 AM#11
Toadcop
arrays are x4 times faster ! or the other way is to itterate group with help of FirstOfGroup()
here link to my XDG system http://xgm.ru/forum/attachment.php?attachmentid=11743
if it needs registration to download it please write it to me !
01-15-2007, 12:20 PM#12
Vexorian
Just 4x times faster? That's not too much considering ForGroup is already fast. I don't get what you mean, Arrays are also faster than FirstOfGroup (which would be quite obvious) or FirstOfGroup is also 4x times faster than ForGroup? (If you want to keep the group you need to iterate twice and that is slower than ForGroup I've tested that
01-15-2007, 12:35 PM#13
Toadcop
Vexorian no you don't undestand =)
x4 times the arrays are faster then to use ForGroup() the FirstOfGroup() iteration is also really fast but i don't really know how quick is GroupRemoveUnit() in different situations... if you have some static groups is really better to use arrays !
01-15-2007, 12:41 PM#14
Vexorian
Remove is the group's advantage. When you want to keep the group FirstOfGroup is even slower than ForGroup, otherwise it is faster.
01-15-2007, 01:10 PM#15
Toadcop
ForGroup is extereme slow ! i don't know why...