HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Which is faster?

06-30-2007, 04:31 PM#1
Ammorth
Im in need of a "Next of Group" function that gets the next unit in the group, and I was wondering which would be faster or if I could optimize one further.

Collapse Method 1:
function NextOfGroup takes group whichGroup, unit currentUnit returns unit
    local group tempGroup = CreateGroup()
    local unit u = null
    set bj_groupAddGroupDest = tempGroup
    call ForGroup(whichGroup, function GroupAddGroupEnum)
    loop
        set u = FirstOfGroup(tempGroup)
        call GroupRemoveUnit(tempGroup, u)
        exitwhen u == currentUnit
    endloop
    set currentUnit = FirstOfGroup(tempGroup)
    if currentUnit == null then
        set currentUnit = FirstOfGroup(whichGroup)
    endif
    
    call DestroyGroup(tempGroup)
    set tempGroup = null
    set u = null
    
    return currentUnit
endfunction


Collapse Method 2:
function NextofGroup_child takes nothing returns nothing
    if GetEnumUnit() == bj_ghoul[283] then
        set bj_groupCountUnits = 1
    elseif bj_groupCountUnits==1 then
        set bj_groupCountUnits = 0
        set bj_ghoul[283] = GetEnumUnit()
    endif
endfunction   

function NextofGroup takes group whichGroup, unit currentUnit returns unit
    set bj_groupCountUnits=0
    set bj_ghoul[283] = currentUnit
    call ForGroup(whichGroup, function NextofGroup_child)
    if bj_ghoul[283] == null then
        set bj_ghoul[283] = FirstOfGroup(whichGroup)
        set bj_groupCountUnits=0
    endif
    return bj_ghoul[283]
endfunction

The first one could be better cause it uses an internal loop and no ForGroup.

The second one may be better still cause it doesn't have to create and fill a new group (handle usage).
06-30-2007, 04:45 PM#2
Anitarf
Look again, the first one uses ForGroup too.
06-30-2007, 04:47 PM#3
Silvenon
But first one also uses ForGroup.......well, anyways I would say the first one bc I've heard ForGroup is slow, but I'm not the expert in these stuff (that means I could be wrong)

EDIT - hehe Anitarf was faster.....
06-30-2007, 05:05 PM#4
Ammorth
Wow, I didn't even realize that. My bad, and thanks guys.
06-30-2007, 05:05 PM#5
grim001
Why do you "need" NextOfGroup? it sounds like you need an array of units, not a unitgroup.
06-30-2007, 05:08 PM#6
Ammorth
Unit groups are quicker and easier to fill/remove than an array of units.

But I wonder if there is not a better way to do this. Let me try and I'll post back.

Edit: No, it didn't work.

My idea was to see if removing and then re-adding units to a unit group would put them to the back of the pack, but it doesn't.

It seems that unit groups sort the group by the handle number, so essentially they are an array of units (only a native version) by their slot (handle).

Interesting. Now I have to think about this one...
06-30-2007, 06:01 PM#7
Anitarf
You could easily implement a linked list with a struct. Simply have a struct that besides the unit also holds data on next and previous struct element. That way, you can easily add units at whichever end, insert them somewhere in between, and remove them from anywhere as well.
06-30-2007, 06:06 PM#8
grim001
Quote:
Originally Posted by Ammorth
Unit groups are quicker and easier to fill/remove than an array of units.

You just need a function to convert a unitgroup into a dynamic array of units... if you're talking about speed, running the function you posted repeatedly is certainly not quick.
06-30-2007, 07:49 PM#9
Ammorth
True to both of those.

I guess if I treat the array as a stack, it shouldn't be a problem, nor be hard to manipulate.