Snippet ForLoopHelper

Captain Grif

New Member
Reaction score
0
vJass script for substantially increasing the speed of looping through integers (gets inlined by JassHelper).

Example use:

JASS:
function X takes nothing returns nothing
    local integer i = 0
    loop
        set i = Increment(i)
        // Do something here.
        exitwhen i>=100
    endloop
    // Something will have been done 100 times.
endfunction


JASS:
library ForLoopHelper

//*********************************************************************
//* ForLoopHelper
//* ----------
//*
//*  This library has been designed to improve the performance of for
//* loops. Whenever you would normally use "set i=i+1" in a loop,
//* replace it with "set i=Increment(i)". Likewise, "set i=i-1" can be
//* replaced with "set i=Decrement(i)". The two functions get inlined
//* resulting in a simple array read, which is 12% faster than adding
//* or subtracting 1 to/from the variable i.
//*
//*  The obvious limitation here is that the value being incremented
//* must not be smaller than 0 or larger than 8190 and it can only be
//* incremented or decremented by 1, but this should be enough to
//* cover nearly all for loops.
//*********************************************************************

    globals
        // This determines how many indexes will be initialized and affects loading time.
        // The number has to be larger than the longest for loop that uses this library.
        // This number may not be larger than 8190, since that is the maximum array size.
        private constant integer MAX_INDEX = 8190
    endglobals

//=====================================================================

    private module Initialization
        static method onInit takes nothing returns nothing
            local integer i=0
            local integer j
            loop
                exitwhen i>MAX_INDEX
                set j=i+1
                set .next<i>=j
                set .prev[j]=i
                set i=j
            endloop
            set .prev[0]=-1
        endmethod
    endmodule
    private struct Inc extends array
        static integer array next
        static integer array prev
        implement Initialization
    endstruct

//=====================================================================

    function Increment takes integer i returns integer
        debug if i&lt;0 or i&gt;8190 then
        debug    call BJDebugMsg(&quot;Increment error: The integer being incremented must not be less than 0 or more than &quot;+I2S(MAX_INDEX)+&quot;.&quot;)
        debug    return i+1
        debug endif
        return Inc.next<i>
    endfunction

    function Decrement takes integer i returns integer
        debug if i&lt;0 or i&gt;8190 then
        debug    call BJDebugMsg(&quot;Decrement error: The integer being decremented must not be less than 0 or more than &quot;+I2S(MAX_INDEX)+&quot;.&quot;)
        debug    return i-1
        debug endif
        return Inc.prev<i>
    endfunction

endlibrary</i></i></i>
 

Nestharus

o-o
Reaction score
84
This depends on how many functions/variables/etc are in the scope. A map with a large amount of variables would actually be slower running on this rather than the +-1.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
I'm actually doubting the initialization function will run fine or not.
Doesn't it hit the opt limit?
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
you people are nuts haha

... but I might use this
 

Romek

Super Moderator
Reaction score
963
So we've reached the point of needing to optimize 'set i = i + 1'. How depressing. :(

> Doesn't it hit the opt limit?
Library initializers are executed.
 

Sevion

The DIY Ninja
Reaction score
424
There really is no point improving the efficiency by a tiny amount on something which is already so fast. This is not where the bottleneck is, was, or should ever be.
 

WolfieeifloW

WEHZ Helper
Reaction score
372
Although I see the point of it not needing to be faster;
Why not use it?
It still improves speed by, what Captain Grif says, 12%.
 

Bankde

Member
Reaction score
20
I'm rather newbie (better say stupid) with these snippets but how do you know it's 12% faster ?
I think using If/then/else and using more function will make it slower doesn't it ?

Will be pleased for answering :)
 

Sevion

The DIY Ninja
Reaction score
424
StopWatches are broken in the latest patch.

The only way that I know of is to use stress testing.
 

Romek

Super Moderator
Reaction score
963
> I think using If/then/else and using more function will make it slower doesn't it ?
When debug mode is disabled, it inlines to a single array read.
 

emjlr3

Change can be a good thing
Reaction score
395
12% increase in speed vs. set i=i+1

not 12% code execution speed increase, maybe .0012%, if that

basically makes your stack act like a linked list IMO, which are slightly faster - or just use a linked list and its even faster then this

There really is no point improving the efficiency by a tiny amount on something which is already so fast. This is not where the bottleneck is, was, or should ever be.

preaching to the choir
 

tooltiperror

Super Moderator
Reaction score
231
I don't know why this has been sitting here so long.

The speed increase is obvious, we've known an array read is faster than an operation forever.

A mod should probably approve this or post...
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
There really is no point improving the efficiency by a tiny amount on something which is already so fast. This is not where the bottleneck is, was, or should ever be.

Agreed. It increases the execute speed, but it doesn't help you at all.
 

tooltiperror

Super Moderator
Reaction score
231
I'd imagine going through, say, a trigger stack 32 times a second may become faster by using this.

EDIT Approved.
 

Nestharus

o-o
Reaction score
84
Ok.. I just stress tested small operations like +,-,*,/ against array reads and the small operations won.

45 fps vs 60 fps (math had yet to go down)


Math is faster than an array read

Looping through an array is faster than looping through a linked list

Myth debunked.



This is officially useless.
 

tooltiperror

Super Moderator
Reaction score
231
Excellent.

I shall now return this script to the land of the fallen.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top