Efficiency question: periodic events

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Which one would work the fastest and has less lag potential if used intensively (Like: i > 100 or sth)?

3 libraries inside this script:

JASS:
// This takes 2 triggers
library TwoSeparates initializer Init
    private function A takes nothing returns nothing
        local integer i = 0
        
        loop
        exitwhen i > 5
            // Approx 20 "A" calls in here
        
            set i = i + 1
        endloop
    endfunction
    
    private function B takes nothing returns nothing
        local integer i = 0
        
        loop
        exitwhen i > 7
            // Approx 20 "B" calls in here
        
            set i = i + 1
        endloop
    endfunction
    
    //=================================================
    private function Init takes nothing returns nothing
        local trigger T = CreateTrigger()
        call TriggerRegisterTimerEvent(T,0.05,true)
        call TriggerAddAction(T,function A)
        
        set T = CreateTrigger()
        call TriggerRegisterTimerEvent(T,0.05,true)
        call TriggerAddAction(T,function B)
    endfunction
endlibrary

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

// This takes one trigger but 2 loops, doubling the calls inside one function
library OneYetSeparate initializer Init
    private function AB takes nothing returns nothing
        local integer i = 0
        
        loop
        exitwhen i > 5
            // Approx 20 "A" calls in here
        
            set i = i + 1
        endloop
        set i = 0
        
        loop
        exitwhen i > 7
            // Approx 20 "B" calls in here
        
            set i = i + 1
        endloop
    endfunction
    
    //=================================================
    private function Init takes nothing returns nothing
        local trigger T = CreateTrigger()
        call TriggerRegisterTimerEvent(T,0.05,true)
        call TriggerAddAction(T,function AB)
    endfunction
endlibrary

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

// This takes one trigger and 1 loop but executes double the calls in one function as well
library OneTogether initializer Init
    private function AB takes nothing returns nothing
        local integer i = 0
        
        loop
        exitwhen i > 7
            if i < 6 then
                // Approx 20 "A" calls in here
            endif
            
            // Approx 20 "B" calls in here
            
            set i = i + 1
        endloop
    endfunction
    
    //=================================================
    private function Init takes nothing returns nothing
        local trigger T = CreateTrigger()
        call TriggerRegisterTimerEvent(T,0.05,true)
        call TriggerAddAction(T,function AB)
    endfunction
endlibrary
 

Technomancer

New Member
Reaction score
14
Option 2 appears to be the cleanest and fastest. Option 1 is the slowest; function calls take the most time, and JASS function calls doubly so, so you need to cut them down.

What do you mean by A and B calls? Can they be inlined as well?
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
A calls = calls that would be made inside function A's loop
B calls = calls that would be made inside function B's loop

So you're saying using 2 loops below each other is faster than option 3: entangling them together?
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
Fastest --> Option 2
Lagless --> Option 1
Useless --> Option 3 //(joke)

Actually, in my own oppinion, a few millisecond won't bother me too much. I'll just rather use the most nice looking code and easy understanding other than those spaghetti code.

Edit -
Hmm, is your 3rd option abit bugged? How come there is 20 A calls without resetting i to 0? Or I mistaken something?
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
No it basically deletes the i > 5 loop, running all its actions inside the i > 7 loop, as long as i isn't greater than 5.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
So: which one is the most lag-free?

100 (5x20) A and 140 (7x20) B calls being executed in their own function?
240 calls being executed in separate loops inside one function?
240 calls being executed inside an intwined loop in one function?


I really need to know because I could have up to 100 calls of that function at once every 0.05 seconds, meaning up to 24.000 calls
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
I'll prefer option 1 for lag free.(100 (5x20) A and 140 (7x20) B calls being executed in their own function)
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
I just realised something, but I'll probably need Vexorian to confirm this :/

Won't this be the fastest possible?
Since it runs every 20 A or B calls separately, disregarding how many thimes those 20 calls are made.
(So for instance, it creates 500 threads doing 20 A calls and 700 threads doing 20 B calls at once, instead of calling 10.000 A calls in one func and 14.000 B calls in another func)

Or will this crash since it starts so many separate threads?

JASS:
library AllSeparateThreads initializer Init

    function A_Actions takes integer i returns nothing
        // 20 "A" calls, using i as it would use i in the previous loop examples
    endfunction

    function B_Actions takes integer i returns nothing
        // 20 "B" calls, using i as it would use i in the previous loop examples
    endfunction

    function A_Callback takes nothing returns nothing
        local integer i = 0

        loop
        exitwhen i > 5
            call A_Actions.execute(i)
            set i = i + 1
        endloop
    endfunction

    function B_Callback takes nothing returns nothing
        local integer i = 0

        loop
        exitwhen i > 7
            call B_Actions.execute(i)
            set i = i + 1
        endloop
    endfunction

    //=================================================
    private function Init takes nothing returns nothing
        local trigger T = CreateTrigger()
        call TriggerRegisterTimerEvent(T,0.05,true)
        call TriggerAddAction(T,function A_Callback)
        
        set T = CreateTrigger()
        call TriggerRegisterTimerEvent(T,0.05,true)
        call TriggerAddAction(T,function B_Callback)
    endfunction
endlibrary
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
So the JASS code in my last post is what I should go for?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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