PDA

View Full Version : [Discussion]Coding efficiency?



The Un-Named
07-05-2007, 11:33 PM
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

Pyro
07-05-2007, 11:58 PM
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 :p always have one. Usually minimum of 20 ms. You wont miss anythign in that time anyway. Mayaswell :p

Ejjman
07-06-2007, 01:10 AM
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..

WhiteShadow
07-06-2007, 03:27 AM
MarkTime(Mark);
repeat
if (GetColor(83, 576) = 255)then
Exit
else
Wait(20);
until (TimeFromMark(Mark) > 6000)

I usually do something like that instead.

Dark_Sniper
07-06-2007, 03:37 AM
to break out of loops the easiest way is to use the while do statement.


var I : Integer;

begin
while (I < 100) do
begin
I:= I + 1;
wait(50)
if (GetColor(83, 576) = 255) then
exit;
end;
end;

Starblaster100
07-06-2007, 08:53 AM
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

The Un-Named
07-06-2007, 11:57 AM
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

Cheesehunk
07-06-2007, 07:11 PM
I setup almost every repeat loop (in walking procedures at least..) something like this:


{.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.

Yakman
07-07-2007, 03:41 PM
nice idea The Un-Named,
iv remembered this, i didnt make it up, but it seemed quite useful if you're a speed whore



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


this is faster



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

Starblaster100
07-07-2007, 03:50 PM
nice idea The Un-Named,
iv remembered this, i didnt make it up, but it seemed quite useful if you're a speed whore



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


this is faster



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

Yakman
07-07-2007, 08:35 PM
oh yes it was Ron, i remember now, in a thread somewhere in the developers section,
hold on ill edit my first post

Ejjman
07-08-2007, 04:58 AM
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...

WhiteShadow
07-08-2007, 05:04 AM
Hmm?

Use Break; then.

Dark_Sniper
07-08-2007, 07:19 PM
whiteshadow is right you answered your own question without knowing it ejj XD


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.

Ejjman
07-09-2007, 06:35 AM
U can break out of a begin..end ffs...omg....

Starblaster100
07-10-2007, 04:32 PM
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... :



Begin
Begin
Writeln('1');
Writeln('2');
Writeln('3');
Writeln('4');
Break;
Writeln('5');
end;
Writeln('We broke out at 4!');
end.


and then:



Var
i: integer;

Begin
For i := 0 to 5 do
Begin
Writeln(IntToStr(i));
If (i = 4) then
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

The Un-Named
07-11-2007, 12:29 AM
Is it just me or has everyone forgot abut Exit;?

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.

shaunthasheep
07-11-2007, 03:26 AM
StarBlaster Semi-Ownt pl0x? ;)

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.

Starblaster100
07-11-2007, 09:20 AM
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 :)

Pyro
07-11-2007, 09:37 AM
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.

The Un-Named
07-13-2007, 04:49 AM
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

Dark_Sniper
07-15-2007, 03:30 AM
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) :p

R0b0t1
07-18-2008, 10:01 PM
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 (http://freepascal.org/), 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.

Nava2
08-06-2008, 05:39 AM
What about Continue; or does that just skip the rest of the loop but still continue with the rest of it...

Nava2

Naum
08-19-2008, 08:56 AM
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.

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;

markuska815
08-19-2008, 11:00 PM
Nauman I think youre wrong on that one...

Continue's are used in loops (yours is not in the actual loop)

What continue does is skips the rest of the loop and moves back to the top:

var
i:integer;
for i:=0 to 5 do
begin
if (i = 4) then
continue;
WriteLn(i);
end;
that will print 0 , 1 , 2, 3 , 5 and skip the 4

ekamjit
10-23-2008, 12:44 AM
i dont understand what ur saying

Blumblebee
10-23-2008, 01:03 AM
i dont understand what ur saying

:fiery: congrads on spamming...

Laze
10-28-2008, 11:12 AM
This is getting weird.. Lol

Laze
10-28-2008, 11:13 AM
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.

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;


I thought this topic concerned advanced scripting?

wonkapwnage
11-02-2008, 02:44 AM
like it! keep it up

Macro_FTW
06-17-2009, 11:13 PM
Sorry for the gravedig. >.<

I don't know a ton about SCAR, but when you initialize arrays in some other languages, space is allocated for every index at that point. So, assuming that SCAR works the same way, would the following procedure in \SRL\Core\Players.Scar be slowing the programs down a small amount, or just using a bit [not the binary kind. :P] of extra RAM?

procedure NumberOfPlayers(Number: Integer);
var
I: Integer;
begin
SetLength(Players, Number);
HowManyPlayers := Number;
for I := 0 to HowManyPlayers - 1 do
begin
SetLength(Players[i].Strings, 100);
SetLength(Players[i].Integers, 100);
SetLength(Players[i].Booleans, 100);
SetLength(Players[i].Extendeds, 100);
end;
end;


And a possible fix would be to manually shrink the size of each array for every player to the size you use?

Of course, this is all [probably incoherent] speculation based on what I know from other languages. XD

~Macro_FTW

marpis
07-10-2009, 07:36 PM
Sorry for the gravedig. >.<


Can't gravedig a tutorial ;)

I just have to say that now im amazed of what computers can do.


program New;
const
a = 98789312654879546216491816372319184646191874367319 18454216108915432724564841542349484543248561771599 87987987987987977987977987987987987987987987987987 987987987987987987198719187178919871879879;
var
i, t, b: integer;
begin
wait(1000);
t := GetSystemTime;
for i := 0 to 1000000 do
b := a shl 94648161978461491981713651665146516511651654156415 44154153164949816195816541654165416541654134312619 48736431915243246191451452914261429142914291429142 941921429142941924192419241929219241924192;
writeln('Done in '+inttostr(GetSystemTime - t)+' ms');
end.

this is calculated in the same time as


program New;
const
a = 5;
i, t, b: integer;
begin
wait(1000);
t := GetSystemTime;
for i := 0 to 1000000 do
b := a shl 3;
writeln('Done in '+inttostr(GetSystemTime - t)+' ms');
end.

(both took around 1600ms)

i also noticed that (a shl 2) and (a*4) are done in the same time, which means that shifting is not actually necessary in simple scripts.

Zyt3x
07-10-2009, 08:17 PM
Yeah, I am impressed too! Think about when we are like 70 years old we will be telling our grandchildren about our 3.20 GHz dual cores witch were amazingly fast :p

Smarter Child
07-20-2009, 03:26 AM
That's if you have grandchildren, or live with your mom your whole life:p

a = 98789312654879546216491816372319184646191874367319 18454216108915432724564841542349484543248561771599 87987987987987977987977987987987987987987987987987 987987987987987987198719187178919871879879;


How is that the same as a := 10?

marpis
07-20-2009, 03:32 AM
That's if you have grandchildren, or live with your mom your whole life:p

a = 98789312654879546216491816372319184646191874367319 18454216108915432724564841542349484543248561771599 87987987987987977987977987987987987987987987987987 987987987987987987198719187178919871879879;


How is that the same as a := 10?

i meant that the calculations were made in the same time with a=987blahblah and a=5. Calculation with the big numbers and the small number both took around 1600ms.