Recursive vs loops

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?
 

Sevion

The DIY Ninja
Reaction score
413
Looping is faster because the recursive call requires you to make a function call (duh) and that's simply just slower than the time it takes to loop.
 

tooltiperror

Super Moderator
Reaction score
231
Recursion has benefits, however. Namely getting past function operation limits.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
Recursion doesn't start new threads? How can recursion bypass the op limit?
 

Sevion

The DIY Ninja
Reaction score
413
You're wrong tooltiperror. The recursive function will hit the op limit and crash the thread because it doesn't start new threads.

In any situation with JASS you should use a loop or timer.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
I think Narks meant if you just use [ljass]call bar()[/ljass]. That would hit the op limit eventually. If you use ExecuteFunc(), then yeah, it won't crash, but it will definitely be a lot slower. xD

>what are the function operation limits o.o?!

It depends on the functions called. Just stay away from massive codes all ran on the same thread. :p (you'll probably encounter this when dealing with strings/save codes)
 

Nestharus

o-o
Reaction score
84
I know in most languages and in general, loops are must more efficient than recursive. Is that right in ALL cases in jass/vJass?

Actually, recursive loops are generally faster than plain loops in most languages.

Here is a more general answer ripped from Yahoo Answers =).
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.


Keep in mind that wc3 op limit is based on memory. If recursive loops take up way more memory, then that means that you will not be able to do as many iterations in a recursive loop as you could in a regular loop for wc3.

Also, keep in mind that arguments in wc3 are strings (parsed into the actual values). This is why calling a function of several parameters has such a massive overhead. In wc3, I'm pretty positive that a function with 1 parameter is going to be much slower for recursion than looping through an array just because of the parsing involved with interpreting the arguments. I'm unsure of a function with no parameters.

Really, it doesn't matter all too much... because it's JASS, I don't recommend recursive loops ;P.
 

Solmyr

Ultra Cool Member
Reaction score
30
Keep in mind that wc3 op limit is based on memory.
I've no idea what you're talking about.

In WC3, each instance of the virtual machine (new one on event, [ljass]TriggerExecute()[/ljass], [ljass]TriggerEvaluate()[/ljass], [ljass]ExecuteFunc()[/ljass] and [ljass]TriggerSleepAction()[/ljass]) has a limit of 300000 operation at continuous execution, at which point the VM returns.

And yes, if you read my post well, you will see that you can use [ljass]TriggerSleepAction()[/ljass] to avoid hitting the op-limit, as it puts the thread on sleep and resets the op-count upon picking it back up.

You can use Grimoire with war3err to dump bytecode as it's being executed.

Here's a list that should contain most (if not all) of the operations:
Code:
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
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top