Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: [Discussion]Coding efficiency?

  1. #1
    Join Date
    Feb 2006
    Posts
    1,022
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default [Discussion]Coding efficiency?

    Well first off, sorry if this is in the wrong section, it seems this section is lifeless and the general board is a bit dead, so I thought I'd liven it up a bit with a discussion in the tuts section about coding efficiency, it could be better in a general section but then everyone can contribute something here so we can all learn something. :P Secondly, it helps my activity.

    Well, so, the main idea of this thread is for some of us coders who are looking to improve how efficiently they code, in other words, how we can code without slowing the computer down by say, forgetting to free Bitmaps and such. A few of my ways I've been trying to cut down on loading too much information when it isn't needed is by freeing all my forms, bitmaps and DTMs, the DTMsbeing pretty recent. Of course, freeing bitmaps, forms and DTMs should all be pretty common, but what I've picked up on more is using more local variables rather than having a few more global variables clogging up the careful clockwork that goes on under SCAR's cover, I'd love it if someone such as Freddy would enlighten me as to what happens to local variables before, and after the procedure/function has been called, I believe they're destroyed after use, and reloaded when needed. Also, what about repeats with no waits? I've seen, and used them before, but I usually have a small wait at an end of a loop, sometimes I've almost crashed because of a lack of waits, despite 2GB ram.

    Obviously this is all pretty common stuff, but I would like to learn more ways to improve my code, and since I've been a bit busy in real life, catch up on some scripting time while I have it. :P

  2. #2
    Join Date
    Feb 2006
    Location
    New Zealand
    Posts
    1,330
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Yeah i usually always keep all my variables as local as i can. Usually only ones that need to be carried over form procedure to another e.g ones that are involved in progress keeping i leave global. Keeps everything nce and compact.

    Freeing everythign up e.g bitmaps and stuff is also very good. And sometimes keeping bitmaps local so that they are free'ed up properly is sometiems good. As often scripts dont end properly so you wont get a chance to free them if they are global.

    Waits are good always have one. Usually minimum of 20 ms. You wont miss anythign in that time anyway. Mayaswell

  3. #3
    Join Date
    Mar 2006
    Location
    United States, -7:00 GMT
    Posts
    1,790
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default

    I didn't know the difference between local and global variables until SB told me whne I released my Fletcher. I am glad I know them now, it really really helps.




    My problems are exiting out of things such simple as

    repeat
    wait(50);
    until(GetColor(83, 576) = 255);


    I don't really know how without it continuing on with the script, and it really disturbs me to put in a whole crapload of stuff. So I need help right there..

    hakuna matata ;)

  4. #4
    Join Date
    Feb 2006
    Location
    L.A, USA
    Posts
    1,632
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Code:
    MarkTime(Mark);
    repeat
      if (GetColor(83, 576) = 255)then
        Exit
      else
        Wait(20);
    until (TimeFromMark(Mark) > 6000)
    I usually do something like that instead.

  5. #5
    Join Date
    Feb 2006
    Location
    Locked in RAM's closet !
    Posts
    2,001
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    to break out of loops the easiest way is to use the while do statement.


    SCAR Code:
    var I : Integer;

    begin
      while (I < 100) do
      begin
        I:= I + 1;
        wait(50)
        if (GetColor(83, 576) = 255) then
          exit;
      end;
    end;
    Darky has stopped by to say hello :).
    10-21-2010
    Updated-
    10-09-2012

  6. #6
    Join Date
    Feb 2006
    Location
    London, England
    Posts
    2,045
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    I do it even simpler than that:

    For i := 0 to 5 do
    Begin
    If (GetColor(1, 2) = 3) then Exit;
    Wait(1000);
    end;

    That way you are guaranteed only a 5 second max loop and the code is pretty short
    SRL Wiki | SRL Rules | SRL Stats
    Ultimate SCAR Scripting Tutorial | Starblaster100's Auth System | Join the official SRL IRC now!


    Help Keep SRL Alive! Please disable Advert Blockers on SRL! Help Keep SRL Alive!


  7. #7
    Join Date
    Feb 2006
    Posts
    1,022
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Yes, I think for .. to .. do are easier, but to break out of a loop you must use "Break;", to exit out of a function/procedure, use "Exit;" :P

  8. #8
    Join Date
    Feb 2006
    Location
    Under a rock.
    Posts
    1,351
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    I setup almost every repeat loop (in walking procedures at least..) something like this:

    Code:
    {.include SRL\SRL.scar}
    var Timers: Integer;
        i: Integer;
    
    begin
      MarkTime(Timers);
      for i := 0 to 25 do
      begin
        if (TimeFromMark(Timers) > 60000) or not LoggedIn then Break;
        WriteLn(IntToStr(i));
        if GetColor(250,10) = 13537628 then i := 25;
      end;
    end.
    Now it's safe against pretty much any mistake that happens, and can continue running with multiple characters.
    SRL Developer
    ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

  9. #9
    Join Date
    Aug 2006
    Location
    London
    Posts
    2,021
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    nice idea The Un-Named,
    iv remembered this, i didnt make it up, but it seemed quite useful if you're a speed whore

    Code:
    for i:=0 to GetArrayLength(MyArray)-1 do
     writeln(MyArray[i]);
    this is faster

    Code:
    len := GetArrayLength(MyArray)-1;
    for i:=0 to len do
     writeln(MyArray[i]);
    its faster causes theres less interpreting, after every pass of the loop, it calls GetArrayLength() and takes away 1.
    but in the next one, its only accesses a variable

    edit: Ron was the first person to point this out, all credit to him
    Join the Official SRL IRC channel. Learn how to Here.

  10. #10
    Join Date
    Feb 2006
    Location
    London, England
    Posts
    2,045
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Yakman View Post
    nice idea The Un-Named,
    iv remembered this, i didnt make it up, but it seemed quite useful if you're a speed whore

    Code:
    for i:=0 to GetArrayLength(MyArray)-1 do
     writeln(MyArray[i]);
    this is faster

    Code:
    len := GetArrayLength(MyArray)-1;
    for i:=0 to len do
     writeln(MyArray[i]);
    its faster causes theres less interpreting, after every pass of the loop, it calls GetArrayLength() and takes away 1.
    but in the next one, its only accesses a variable
    I think Ron was the first one to point that out
    SRL Wiki | SRL Rules | SRL Stats
    Ultimate SCAR Scripting Tutorial | Starblaster100's Auth System | Join the official SRL IRC now!


    Help Keep SRL Alive! Please disable Advert Blockers on SRL! Help Keep SRL Alive!


  11. #11
    Join Date
    Aug 2006
    Location
    London
    Posts
    2,021
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    oh yes it was Ron, i remember now, in a thread somewhere in the developers section,
    hold on ill edit my first post
    Join the Official SRL IRC channel. Learn how to Here.

  12. #12
    Join Date
    Mar 2006
    Location
    United States, -7:00 GMT
    Posts
    1,790
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default

    Can u break out a begin?

    procedure Blah;
    var
    i : integer;
    begin
    for i := 0 to 5 do
    begin
    writeln(IntToStr(i));
    if(i = 4)then Exit;
    end;
    writeln('After i = 4, come here');
    end;

    Do you understand? If you Exit, it exits the whole procedure/function. Without a Repeat...

    hakuna matata ;)

  13. #13
    Join Date
    Feb 2006
    Location
    L.A, USA
    Posts
    1,632
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Hmm?

    Use Break; then.

  14. #14
    Join Date
    Feb 2006
    Location
    Locked in RAM's closet !
    Posts
    2,001
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    whiteshadow is right you answered your own question without knowing it ejj XD


    SCAR Code:
    procedure Blah;
    var
      i : integer;
    begin
      for i := 0 to 5 do
      begin
        writeln(IntToStr(i));
        if(i = 4)then break;
      end;
      writeln('After i = 4, come here');
    end;

    begin
      blah;
    end.
    Darky has stopped by to say hello :).
    10-21-2010
    Updated-
    10-09-2012

  15. #15
    Join Date
    Mar 2006
    Location
    United States, -7:00 GMT
    Posts
    1,790
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default

    U can break out of a begin..end ffs...omg....

    hakuna matata ;)

  16. #16
    Join Date
    Feb 2006
    Location
    London, England
    Posts
    2,045
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Ejjman1 View Post
    U can break out of a begin..end ffs...omg....
    no you cannot. (unless this has been changed in Divi 3.10 [which i doubt] )

    You can break out of a While .. Do loop, or a Repeat .. Until loop, or a For .. to .. do loop, but Never a Begin .. end; loop.

    It just so happens, "For to do" and "While do" wrap around a "begin end" and that is why you can break out of it.

    try running the following. Unfortunately, i do not have access to SCAR atm, so i cannot prove myself right or wrong. However... :

    PHP Code:
    Begin
      Begin
        Writeln
    ('1');
        
    Writeln('2');
        
    Writeln('3');
        
    Writeln('4');
        Break;
        
    Writeln('5');
      
    end;
      
    Writeln('We broke out at 4!');
    end
    and then:

    PHP Code:
    Var
      
    iinteger;

    Begin
      
    For := 0 to 5 do
      
    Begin
        Writeln
    (IntToStr(i));
        If (
    4then
          
    Break;
      
    end;
      
    Writeln('We broke out at 4!');
    end
    Sorry if it seems like a rant, it's not supposed to be; i've had a tough day. After all, we're all on a steep learning curve here
    SRL Wiki | SRL Rules | SRL Stats
    Ultimate SCAR Scripting Tutorial | Starblaster100's Auth System | Join the official SRL IRC now!


    Help Keep SRL Alive! Please disable Advert Blockers on SRL! Help Keep SRL Alive!


  17. #17
    Join Date
    Feb 2006
    Posts
    1,022
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Is it just me or has everyone forgot abut Exit;?

    SCAR Code:
    procedure BreakOutOfProcedure;
    begin
      Exit;
      Writeln('procedure BreakOutOfProcedure;');
    end;

    procedure BreakOutOfLoop;
    var
      I : Integer;
    begin
      for I := 1 to 10 do
      begin
        Break;
        Writeln('procedure BreakOutOfLoop;');
      end;
    end;

    procedure BreakOutOfProcedureAndLoop;
    var
      I : Integer;
    begin
      for I := 1 to 10 do
      begin
        Exit;
        Writeln('procedure BreakOutOfProcedureAndLoop;');
      end;
      Writeln('procedure BreakOutOfProcedureAndLoop;');
    end;

    begin
      BreakOutOfLoop;
      BreakOutOfProcedure;
      BreakOutOfProcedureAndLoop;
    end.

  18. #18
    Join Date
    May 2006
    Posts
    1,230
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    StarBlaster Semi-Ownt pl0x?

    SCAR Code:
    program New;
    label
      MyJump;

    begin

      Writeln('First begin');

      begin
        Writeln('  Breaking Out');
        GoTo MyJump;
        Writeln('WTF?');
      end;

      MyJump:
      Writeln('Second Begin');
      begin
        Writeln('  OMFG WE JUST BROKE OUT!');
      end;
    end.

  19. #19
    Join Date
    Feb 2006
    Location
    London, England
    Posts
    2,045
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    Thats not a break, since it doesn't have the word "Break" anywhere in the script.

    Un-Named, no one has forgotten about Exit, we were just concentrating on the others more
    SRL Wiki | SRL Rules | SRL Stats
    Ultimate SCAR Scripting Tutorial | Starblaster100's Auth System | Join the official SRL IRC now!


    Help Keep SRL Alive! Please disable Advert Blockers on SRL! Help Keep SRL Alive!


  20. #20
    Join Date
    Feb 2006
    Location
    New Zealand
    Posts
    1,330
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Star is 100% correct on this one. This whole thing came from someone using a exit when they should have used a break.... Or so it looked. May have not even been that way. But star is completely correct. Break is used for breaking out of LOOPS! and exits are used for procedures and functons.

  21. #21
    Join Date
    Feb 2006
    Posts
    1,022
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by The Un-Named View Post
    Yes, I think for .. to .. do are easier, but to break out of a loop you must use "Break;", to exit out of a function/procedure, use "Exit;" :P
    But my post came first. :P

  22. #22
    Join Date
    Feb 2006
    Location
    Locked in RAM's closet !
    Posts
    2,001
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by c0de View Post
    Begin End is not a loop.
    your right there.

    i never knew that loops were so complicated... breaking out of loops may be complicated though (depends though)
    Darky has stopped by to say hello :).
    10-21-2010
    Updated-
    10-09-2012

  23. #23
    Join Date
    Dec 2006
    Location
    Banville
    Posts
    3,914
    Mentioned
    12 Post(s)
    Quoted
    98 Post(s)

    Default

    In assembly, it would make sense if everything broke down into a while or repeat loop. This is because these structures are fairly easy, and advanced ones like for loops add steps generated by the compiler. Thus, this would seem fastest to adapt you're own ideas to use while or until loops.


    If you really want to make stuff faster, write a plugin and turn on EVERY SINGLE COMPILER BASED OPTIMIZATION IT OFFERS (I suggest FPC/Lazarus, pretty close to full Delphi). If it is still not fast enough... Then read in to human-based optimization. And if its still not fast enough... Why the hell are you trying to make it this fast? Use inline assembly. Don't know how? Learn C/C++ and make plugins in that and take the same steps.

    And you really shouldn't need to make it so fast... Because in most cases, it already fast enough or as fast as it will ever get.
    The jealous temper of mankind, ever more disposed to censure than
    to praise the work of others, has constantly made the pursuit of new
    methods and systems no less perilous than the search after unknown
    lands and seas.

  24. #24
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    What about Continue; or does that just skip the rest of the loop but still continue with the rest of it...

    Nava2
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  25. #25
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by Nava2 View Post
    What about Continue; or does that just skip the rest of the loop but still continue with the rest of it...

    Nava2

    Continue
    will keep on running through the loop until a result is true or it is told to break.

    Its used lots in TPA finding.

    SCAR Code:
    If FindColorsTolerance(Blah, Blah) Then
      Atpa := TPAtoATPA(TPA, 9);

      For i := 0 to High(ATPA) Do
        C := MiddleTPA(ATPA[I]);
         Begin
           MMouse(c.x, c.y, 1, 1);
           Result := True;
         end else
           Continue //until it finds the object or is told to break;
    end;

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Function coding help please :)
    By Kosaki in forum OSR Help
    Replies: 3
    Last Post: 01-04-2008, 04:00 PM
  2. Logic and Efficiency
    By munk in forum OSR Outdated Tutorials
    Replies: 6
    Last Post: 11-17-2007, 12:32 AM
  3. Need some help with coding
    By pomme in forum OSR Help
    Replies: 2
    Last Post: 10-09-2007, 01:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •