• 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.

[vJASS] Which one is better, "runtextmacro" or loop?

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi guys, please take a look at the short example below:
JASS:
    //! textmacro T takes N
    call SaveUnitHandle(hash,GetHandleId(t),$N$,dum[$N$])
    //! endtextmacro
    //! runtextmacro T("0")
    //! runtextmacro T("1")
    //! runtextmacro T("2")
    //! runtextmacro T("3")
    //! runtextmacro T("4")
    //! runtextmacro T("5")

//---------------------------------------------
  
    local integer i = 0
    loop
        exitwhen i > 5
        call SaveUnitHandle(hash,GetHandlId(t),i,dum[i])
        set i = i + 1
    endloop

I have three questions:
1. In terms of the time of execution, which one is faster?
2. As for efficiency, which one is better?
3. Which one would you use? :)

Thanks.
 
Level 21
Joined
Mar 27, 2012
Messages
3,232
Hi guys, please take a look at the short example below:
JASS:
    //! textmacro T takes N
    call SaveUnitHandle(hash,GetHandleId(t),$N$,dum[$N$])
    //! endtextmacro
    //! runtextmacro T("0")
    //! runtextmacro T("1")
    //! runtextmacro T("2")
    //! runtextmacro T("3")
    //! runtextmacro T("4")
    //! runtextmacro T("5")

//---------------------------------------------
  
    local integer i = 0
    loop
        exitwhen i > 5
        call SaveUnitHandle(hash,GetHandlId(t),i,dum[i])
        set i = i + 1
    endloop

I have two questions:
1. In terms of the time of execution, which one is faster?
2. As for efficiency, which one is better?

Thanks.

This won't compile, but whatever.

Essentially the question is whether a loop is faster than a sequence of actions? - No, I think it'll never be.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
1. loop will always be slower, because it has to jump back in the instructions from endloop to loop and also has to evaluate the exitwhen conditions at every pass whereas sequential call of the functions will never need to do neither of those things

2. the same

3. I would still loop through, cause its easier to write, easier to read and more clean code with its intention explicitly stated whereas the textmacroes are pretty hard to read most of the time

Also textmacroes take longer to compile than loops
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
Textmacro just changes what you write to actual jass code when you save your map, it doesn't run in game.
 
Level 11
Joined
Oct 11, 2012
Messages
711
1. loop will always be slower, because it has to jump back in the instructions from endloop to loop and also has to evaluate the exitwhen conditions at every pass whereas sequential call of the functions will never need to do neither of those things

2. the same

3. I would still loop through, cause its easier to write, easier to read and more clean code with its intention explicitly stated whereas the textmacroes are pretty hard to read most of the time

Also textmacroes take longer to compile than loops

Textmacro just changes what you write to actual jass code when you save your map, it doesn't run in game.

Thanks for both of you. +Rep if I can.
 
Use a loop where the loop is convenient. Don't consider speed for something trivial like that (don't take this offensively, it is a good question :D).

Keep in mind that the textmacros are a vJASS feature for code condensation (mostly). In the end, they still end up as regular JASS. So it would still end up as:
JASS:
call SaveUnitHandle(hash,GetHandleId(t),1,dum[1])
call SaveUnitHandle(hash,GetHandleId(t),2,dum[2])
call SaveUnitHandle(hash,GetHandleId(t),3,dum[3])
call SaveUnitHandle(hash,GetHandleId(t),4,dum[4])
call SaveUnitHandle(hash,GetHandleId(t),5,dum[5])
call SaveUnitHandle(hash,GetHandleId(t),6,dum[6])

So in the end, you aren't asking whether a textmacro is better than a loop, but whether a sequence of functions is better than the loop counterpart. For small loops, 2-3 loops, I might consider the functions, but otherwise I recommend a loop: (1) it is good coding practice (2) easy to change (3) that is what they're for (4) usually you end up with smaller code = lower filesize (even though it is a matter of bits, I'd take a few bits/bytes over a microsecond of speed in wc3, usually).
 
Level 11
Joined
Oct 11, 2012
Messages
711
Use a loop where the loop is convenient. Don't consider speed for something trivial like that (don't take this offensively, it is a good question :D).

Keep in mind that the textmacros are a vJASS feature for code condensation (mostly). In the end, they still end up as regular JASS. So it would still end up as:
JASS:
call SaveUnitHandle(hash,GetHandleId(t),1,dum[1])
call SaveUnitHandle(hash,GetHandleId(t),2,dum[2])
call SaveUnitHandle(hash,GetHandleId(t),3,dum[3])
call SaveUnitHandle(hash,GetHandleId(t),4,dum[4])
call SaveUnitHandle(hash,GetHandleId(t),5,dum[5])
call SaveUnitHandle(hash,GetHandleId(t),6,dum[6])

So in the end, you aren't asking whether a textmacro is better than a loop, but whether a sequence of functions is better than the loop counterpart. For small loops, 2-3 loops, I might consider the functions, but otherwise I recommend a loop: (1) it is good coding practice (2) easy to change (3) that is what they're for (4) usually you end up with smaller code = lower filesize (even though it is a matter of bits, I'd take a few bits/bytes over a microsecond of speed in wc3, usually).

I was expecting you to answer my questions, PnF. LOL
Thanks, I will use a loop then, +Rep.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Try to restrict yourself with textmacros in general. They are there to fix linguistic weaknesses and battle tedious repetitions. But they are so mighty and arbitrary that they can jumble up the syntax, destroy readability etc. And yeah, execution time is not the only factor, you want the code to be adjustable later on, clear and make progress in your project. If you want to change something later on, and you most likely will because not everything is concepted in detail and it would be boring, having deadlocked codes then, you will see that you can as well write everything anew.
 
Status
Not open for further replies.
Top