• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Too much arrays

Status
Not open for further replies.
Level 7
Joined
Apr 5, 2011
Messages
245
Hello!

Here is my code:
JASS:
        //! textmacro MANA_LOSS_PROCESSOR takes temp
            set Support.workInteger = 0
            loop
                exitwhen Support.workInteger == toadCount
                set Support.workReal = GetUnitState(toad[Support.workInteger], UNIT_STATE_MANA)
                set $temp$ = toadMana[Support.workInteger] - Support.workReal
                set toadMana[Support.workInteger] = Support.workReal
                if toadManaLossCount[Support.workInteger] + 1 == MANA_LOSS_MAX_COUNT then
                    set toadManaLossCount[Support.workInteger] = 0
                else
                    set toadManaLossCount[Support.workInteger] = toadManaLossCount[Support.workInteger] + 1
                endif
                if $temp$ < 0 then
                    set $temp$ = 0
                endif
                if toadManaLossBuffer[Support.workInteger] != 0 then
                    set Support.workReal = toadManaLossBuffer[Support.workInteger] - toadManaLoss[Support.workInteger][toadManaLossCount[Support.workInteger]]
                    if Support.workReal < 0 then
                        set toadManaLossBuffer[Support.workInteger] = 0
                        set toadManaLossTotal[Support.workInteger] = toadManaLossTotal[Support.workInteger] + Support.workReal + $temp$
                    else
                        set toadManaLossBuffer[Support.workInteger] = Support.workReal
                        set toadManaLossTotal[Support.workInteger] = toadManaLossTotal[Support.workInteger] + $temp$
                    endif
                endif
                if toadManaLossTotal[Support.workInteger] >= MANACOST then
                    set toadManaLossBuffer[Support.workInteger] = toadManaLossBuffer[Support.workInteger] + MANACOST
                    set toadManaLossTotal[Support.workInteger] = toadManaLossTotal[Support.workInteger] - MANACOST
                    call spawnToadling()
                endif
                set toadManaLoss[Support.workInteger][toadManaLossCount[Support.workInteger]] = $temp$
                set Support.workInteger = Support.workInteger + 1
            endloop
        //! endtextmacro

The question is:
am I right, those arrays not only look bad (it does not matter for this question!), but also inefficient? So it would work faster if I use local variables instead of 10 times addressing same array (especially 2D). Or no?
 
Hello!

Here is my code:
JASS:
        //! textmacro MANA_LOSS_PROCESSOR takes temp
            set Support.workInteger = 0
            loop
                exitwhen Support.workInteger == toadCount
                set Support.workReal = GetUnitState(toad[Support.workInteger], UNIT_STATE_MANA)
                set $temp$ = toadMana[Support.workInteger] - Support.workReal
                set toadMana[Support.workInteger] = Support.workReal
                if toadManaLossCount[Support.workInteger] + 1 == MANA_LOSS_MAX_COUNT then
                    set toadManaLossCount[Support.workInteger] = 0
                else
                    set toadManaLossCount[Support.workInteger] = toadManaLossCount[Support.workInteger] + 1
                endif
                if $temp$ < 0 then
                    set $temp$ = 0
                endif
                if toadManaLossBuffer[Support.workInteger] != 0 then
                    set Support.workReal = toadManaLossBuffer[Support.workInteger] - toadManaLoss[Support.workInteger][toadManaLossCount[Support.workInteger]]
                    if Support.workReal < 0 then
                        set toadManaLoss[Support.workInteger][toadManaLossCount[Support.workInteger]] = -Support.workReal
                        set toadManaLossBuffer[Support.workInteger] = 0
                    else
                        set toadManaLoss[Support.workInteger][toadManaLossCount[Support.workInteger]] = 0
                        set toadManaLossBuffer[Support.workInteger] = Support.workReal
                    endif
                endif
                set toadManaLossTotal[Support.workInteger] = toadManaLossTotal[Support.workInteger] - toadManaLoss[Support.workInteger][toadManaLossCount[Support.workInteger]] + $temp$
                if toadManaLossTotal[Support.workInteger] >= MANACOST then
                    set toadManaLossBuffer[Support.workInteger] = toadManaLossBuffer[Support.workInteger] + MANACOST
                    set toadManaLossTotal[Support.workInteger] = toadManaLossTotal[Support.workInteger] - MANACOST
                    call spawnToadling()
                endif
                set toadManaLoss[Support.workInteger][toadManaLossCount[Support.workInteger]] = $temp$
                set Support.workInteger = Support.workInteger + 1
            endloop
        //! endtextmacro

The question is:
am I right, those arrays not only look bad (it does not matter for this question!), but also inefficient? So it would work faster if I use local variables instead of 10 times addressing same array (especially 2D). Or no?

2D arrays are converted to regular arrays during pre-processing, because JASS does not natively support 2D arrays - so the difference in performance between the two is 0.

Referencing arrays is relatively cheap, and this trigger size is not enough to justify creating locals. I haven't tested myself, but I suspect there's almost never a case when creating a local handle for an array reference is faster (due to operation limits).

If your script works, be happy.
 
Status
Not open for further replies.
Top