Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Optimize GUI code - how to?

Discussion in 'Triggers & Scripts' started by CaptainHero, Oct 24, 2014.

  1. CaptainHero

    CaptainHero

    Joined:
    Sep 30, 2012
    Messages:
    197
    Resources:
    1
    Spells:
    1
    Resources:
    1
    For now i'm doing GUI, after some more testing and changes i'm going to translate and optimize it into vjass..

    however i already wanted to optimize the GUI code a bit now..

    So i got some questions:

    Is a if-then-else tree like this good?
    So basically it will checks for the right condition till it is found?

    • Untitled Trigger 001
      • Events
      • Conditions
      • Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Integer Equal to 1
          • Then - Actions
          • Else - Actions
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • Integer Equal to 2
              • Then - Actions
              • Else - Actions
                • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                  • If - Conditions
                    • Integer Equal to 3
                  • Then - Actions
                  • Else - Actions
                    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                      • If - Conditions
                        • Integer Equal to 4
                      • Then - Actions
                      • Else - Actions
                        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                          • If - Conditions
                            • Integer Equal to 5
                          • Then - Actions
                          • Else - Actions


    or is this better?
    kinda trying to get to my final questions faster, by adding some questions down at the root of the three. Like if integer is bigger than, or smaller.
    So the question is can more code, be more efficient?

    • Untitled Trigger 001
      • Events
      • Conditions
      • Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Integer Less than or equal to 2
          • Then - Actions
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • Integer Greater than 1
              • Then - Actions
              • Else - Actions
                • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                  • If - Conditions
                    • Integer Equal to 2
                  • Then - Actions
                  • Else - Actions
          • Else - Actions
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • Integer Greater than or equal to 3
              • Then - Actions
              • Else - Actions
                • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                  • If - Conditions
                    • Integer Equal to 3
                  • Then - Actions
                  • Else - Actions
                    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                      • If - Conditions
                        • Integer Equal to 4
                      • Then - Actions
                      • Else - Actions
                        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                          • If - Conditions
                            • Integer Equal to 5
                          • Then - Actions
                          • Else - Actions


    and if checking for unit types does the order make a difference?
    Like is he counting up? - asking for: if its 'h001', then 'h002' and then 'h003' in that order anyway? So it would be important that i ask for those units also in that order?

    otherwise if i ask for 'h003' at start - would he have already checked 'h001' and 'h002' anyway?
     
  2. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,537
    Resources:
    23
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    5
    JASS:
    3
    Resources:
    23
    You obviously mean JASS here. vJASS is just again an other abstraction of JASS.

    If really you really need like 5-6 "elseif"s just check them in a linear way. (in JASS it looks better than in GUI :p)
    There is a more efficient way, if you have quite a lot linear checks. You always split the amount of needed checks in middle, but this will get more unreadable and you won't have too much benefits.

    But when ever you can, you should try to avoid many nested if/then/else blocks and use a shorter, more readable loop instead... and using indices within.

    I'm not really sure I understand your second question about unit type comparisson.
    Are you talking about GUI or JASS? Because in GUI you can only compare if two types are equal or not.
    In JASS you can use unitTypes like what they actually are (integers). So then you of course also can make comparissons with "<" and ">".
     
  3. deathismyfriend

    deathismyfriend

    Joined:
    Oct 24, 2012
    Messages:
    6,526
    Resources:
    14
    Spells:
    12
    Tutorials:
    2
    Resources:
    14
    Something like this is hard to say which is best. I would go with option 1. For the number most used keep it at the top so in the ITEs if 3 is the most common then start with 3 instead of 1.
     
  4. CaptainHero

    CaptainHero

    Joined:
    Sep 30, 2012
    Messages:
    197
    Resources:
    1
    Spells:
    1
    Resources:
    1
    okay thanks good to know i got like 20-30 if-then-else trees

    3. i meant if you got 3 custom units and their "raw-data" name is h001, h002 and h003.

    should i check them in that order?
    like if he would check for h001, and counts up, and its not h001, does the pc then proceeds, still counting up from h001? so it would be faster to get them already in the right order? so is it a bit like teletext?
     
  5. deathismyfriend

    deathismyfriend

    Joined:
    Oct 24, 2012
    Messages:
    6,526
    Resources:
    14
    Spells:
    12
    Tutorials:
    2
    Resources:
    14
    Why would you need to check them ? In most cases you do nt need to check specific units like that. Can you post the trigger so I can better understand what you are doing and how to optimize it for you.
     
  6. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    26,134
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    Ah the good old case statement! Too bad JASS has no case statement lol...

    Depending on the sort of data being checked and what you want done you could create a data fed system to manage it. It might have more overhead but it will scale a lot better and be maintainable.

    For example, if you testing unit types for specific values then you could use a hashtable where the unit type is an index and the value stored is your value. Hashtables have very good O(1) style performance up to a reasonable number (several thousand) of entries. This will be far faster than any kind of tree structure of comparisons as the JASS interpreter overhead is so high.

    You can represent dynamic code as triggers. Speed wise it does have some overhead running the trigger but this is highly flexible and often more scalable. You can pass arguments to the dynamic code using global variables as registers as WC3 is single threaded so has well defined execution order with no pre-emptive capabilities.