• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Let's play "Which code is faster?"

Status
Not open for further replies.
Hi everyone,

some parts of my script are run several thousand times per second, so every bit of optimization is useful. I have three questions:


1.
JASS:
call BlzSetSpecialEffectPosition( T_A.visual , T_A.x  , T_A.y , T_A.z )
vs.
JASS:
call BlzSetSpecialEffectX( T_A.visual , T_A.x )
call BlzSetSpecialEffectY( T_A.visual , T_A.y )
With z being constant, which one is faster? I vaguely remember that for units, the second method is faster, but iirc it was because it doesn't check collisions. So I would think that the first method is faster.


2.
JASS:
set whichTrigger = CreateTrigger()
call TriggerAddCondition( whichTrigger , PARTICLE_CONTACT_FUNC[T_A.particleType][T_B.particleType] )
call TriggerEvaluate(whichTrigger)
call DestroyTrigger(whichTrigger)
vs.
JASS:
call TriggerAddCondition( whichTrigger , PARTICLE_CONTACT_FUNC[T_A.particleType][T_B.particleType] )
call TriggerEvaluate(whichTrigger)
call TriggerRemoveCondition(whichTrigger , PARTICLE_CONTACT_FUNC[T_A.particleType][T_B.particleType]) // or TriggerClearConditions(whichTrigger)
I wasn't aware that TriggerRemoveCondition existed, so I didn't think about using it. But I would guess that it's much faster since it avoids creating a handle each time.


3. Something I've always wondered is how expensive are array lookups and when does it become viable to store the array variable in a new non-array variable. For example, which one is faster?
JASS:
set B = A[I] + 1
set C = 2*A[I]
set D = SquareRoot(A[I])
vs.
JASS:
set Ai = A[I]
set B = Ai + 1
set C = 2*Ai
set D = SquareRoot(Ai)

Thanks for any help!
 
Most of my answers here are speculative (without benchmark tests), so here goes:
  1. BlzSetSpecialEffectPosition might be more efficient or faster to use here. Special Effects don't have collision after all.

  2. I think the latter might be less expensive in the long run, having to generate only one handle. However, I'm not sure if the given code will compile correctly, since TriggerRemoveCondition expects a triggercondition as the second parameter, which you can retrieve from TriggerAddCondition.

  3. Calculating the correct address to read memory from might cost you some efficiency where it matters (as it does in this scenario), so the latter option appears to be more viable (storage wise). Not sure about the stack, though.
 
[*]I think the latter might be less expensive in the long run, having to generate only one handle. However, I'm not sure if the given code will compile correctly, since TriggerRemoveCondition expects a triggercondition as the second parameter, which you can retrieve from TriggerAddCondition.
Oh, I misread the function. I thought it expected a conditionfunc, but it expects a "triggercondition". So, the code would be either
JASS:
set T_condition = TriggerAddCondition( whichTrigger , PARTICLE_CONTACT_FUNC[T_A.particleType][T_B.particleType] )
call TriggerEvaluate(whichTrigger)
call TriggerRemoveCondition(whichTrigger , T_condition)
or
JASS:
call TriggerAddCondition( whichTrigger , PARTICLE_CONTACT_FUNC[T_A.particleType][T_B.particleType] )
call TriggerEvaluate(whichTrigger)
call TriggerClearConditions(whichTrigger)
Thanks a lot!
 
Anyone has some more thoughts on the array-lookup speed? If I'm not wrong about how this sthuff works, there should be a break-even point, where storing the array should be faster than having the array-index each time, but where is that point? How would you set up a benchmark to test it?

JASS:
//With which value for N is the second method is faster?

set L = 1
loop
    set B = A[I] * 1.23456789
    set L = L + 1
    exitwhen L > N
endloop

//vs.

set L = 1
set Ai = A[I]
loop
    set B = Ai * 1.23456789
    set L = L + 1
    exitwhen L > N
endloop
 
You should read through JASS Benchmarking Results and maybe tag @DracoL1ch if your question isn't already answered there
Thanks!

I just tried an fps test myself, and my result was that if the array variable is referenced more than 4 times, it's faster to store it in a different variable first (at 4 I had the same fps, at 10 I had 20 more fps with the storing first method).
 
Status
Not open for further replies.
Top