Infinitegde
O.O
- Reaction score
- 86
I know in most languages and in general, loops are must more efficient than recursive. Is that right in ALL cases in jass/vJass?
function foo
local integer x=0
loop
set x=x+1
call BJDebugMsg(I2S(x*2+1))
endloop
endfunction
//=========================================
globals
integer x
endglobals
function bar takes nothing returns nothing
set x=x+1
call BJDebugMsg(I2S(x))
call ExecuteFunc("bar")
endfunction
I know in most languages and in general, loops are must more efficient than recursive. Is that right in ALL cases in jass/vJass?
This depends on the language being used. You wrote 'language-agnostic', so I'll give some examples.
In Java, C, and Python, recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame. In some C compilers, one can use a compiler flag to eliminate this overhead, which transforms certain types of recursion (actually, certain types of tail calls) into jumps instead of function calls.
In functional programming language implementations, sometimes, iteration can be very expensive and recursion can be very cheap. In many, recursion is transformed into a simple jump, but changing the loop variable (which is mutable) sometimes requires some relatively heavy operations, especially on implementations which support multiple threads of execution. Mutation is expensive in some of these environments because of the interaction between the mutator and the garbage collector, if both might be running at the same time.
I know that in some Scheme implementations, recursion will generally be faster than looping.
In short, the answer depends on the code and the implementation. Use whatever style you prefer. If you're using a functional language, recursion might be faster. If you're using an imperative language, iteration is probably faster. In some environments, both methods will result in the same assembly being generated (put that in your pipe and smoke it).
Addendum: In some environments, the best alternative is neither recursion nor iteration but instead higher order functions. These include "map", "filter", and "reduce" (which is also called "fold"). Not only are these the preferred style, not only are they often cleaner, but in some environments these functions are the first (or only) to get a boost from automatic parallelization — so they can be significantly faster than either iteration or recursion. Data Parallel Haskell is an example of such an environment.
List comprehensions are another alternative, but these are usually just syntactic sugar for iteration, recursion, or higher order functions.
I've no idea what you're talking about.Keep in mind that wc3 op limit is based on memory.
enum OPCODES { OP_ENDPROGRAM=0x1,
OP_FUNCTION=0x3, // _ _ rettype funcname
OP_ENDFUNCTION=0x4,
OP_LOCAL=0x5, // _ _ type name
OP_GLOBAL=0x6,OP_CONSTANT=0x7,
OP_POPFUNCARG=0x8, // _ srcargi type destvar
OP_CLEANSTACK=0xB, // _ _ nargs _
OP_LITERAL=0xC, // _ type destreg srcvalue
OP_SETRET=0xD, // _ srcreg _ _
OP_GETVAR=0xE, // _ type destreg srcvar
OP_CODE=0xF,
OP_GETARRAY=0x10,
OP_SETVAR=0x11, // _ _ srcreg destvar
OP_SETARRAY=0x12,
OP_PUSH=0x13, // _ _ srcreg _
OP_SETRIGHT=0x14,
OP_NATIVE=0x15, // _ _ _ fn
OP_JASSCALL=0x16, // _ _ _ fn
OP_I2R=0x17,
OP_AND = 0x18,
OP_OR = 0x19,
OP_EQUAL=0x1A,
OP_NOTEQUAL=0x1B, // check
OP_LESSEREQUAL=0x1C,OP_GREATEREQUAL=0x1D,
OP_LESSER=0x1E,OP_GREATER=0x1F,
OP_ADD=0x20,OP_SUB,OP_MUL,OP_DIV,
OP_MODULO = 0x24, // unused
OP_NEGATE=0x25,
OP_NOT = 0x26,
OP_RETURN=0x27, // _ _ _ _
OP_JUMPTARGET=0x28,
OP_JUMPIFTRUE=0x29,OP_JUMPIFFALSE=0x2A,
OP_JUMP=0x2B
}