forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


optimising code
Goto page 1, 2  Next
 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
Serge
Professional Member
Professional Member


Joined: 04 Mar 2002
Posts: 1480
Location: Australia

PostPosted: Wed Mar 27, 2002 7:46 am    Post subject: optimising code Reply with quote

Can someone explain what optimising code means and how it applies to VDS?

I gather that it means that you make the code run as fast as possible using as few resources as possible, but how do you do that with VDS?

And given that CPU's are so fast today, does it really matter whether code is optimised or not?

Serge

_________________
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
SnarlingSheep
Professional Member
Professional Member


Joined: 13 Mar 2001
Posts: 759
Location: Michigan

PostPosted: Wed Mar 27, 2002 9:01 am    Post subject: Reply with quote

During and after you write a program, look through it and see if you can do some of the things you are doing, with less code or possibly faster.
I think you should always want to make your programs run as fast and as smoothly as possibly, and there will always be users with slow computers out there.
Having less code makes it easier to edit/update/debug for you anyway Smile

Something I used to do was like:
Code:

if @EQUAL(%%Blah,Blah)
  if @EQUAL(%%Blah2,Blah)
    goto Blah
  end
end

Little less code to use:
Code:

if @BOTH(@EQUAL(%%Blah,Blah),@EQUAL(%%Blah2,Blah))
  goto Blah
end

I also try to use REPEAT wherever possible.
If you have a row of BMP's, something like this would be better than 20 DIALOG ADD's:
Code:

  %%Num = 1
  %%Posx = 10
  repeat
    DIALOG ADD,BITMAP,BMP%%Num,0,%%PosX,32,32,C:\blah.bmp
    %%Posx = @FADD(%%Posx,@FMUL(33,%%Num))
    %%Num = @SUCC(%%Num)
  until @GREATER(%%Num,20)

Just my 2¢ on this Smile

_________________
-Sheep
My pockets hurt...
Back to top
View user's profile Send private message Send e-mail
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1564

PostPosted: Wed Mar 27, 2002 3:11 pm    Post subject: Reply with quote

Quote:
Code:
 
%%Num = 1
  %%Posx = 10
  repeat
    DIALOG ADD,BITMAP,BMP%%Num,0,%%PosX,32,32,C:\blah.bmp
    %%Posx = @FADD(%%Posx,@FMUL(33,%%Num))
    %%Num = @SUCC(%%Num)
  until @GREATER(%%Num,20)


While that is less lines of code it may take longer to create the bitmaps then it would with 20 dialog add commands. VDS is an interpreted langauge and each function/command you use has to be interpreted, as well as variables. I see up to 4 functions, and 3 variables you use there, with each having to be interpreted. It might be slightly faster to just use the 20 lines of DIALOG ADD without variables and functions that have to be interpreted.
Back to top
View user's profile Send private message
SnarlingSheep
Professional Member
Professional Member


Joined: 13 Mar 2001
Posts: 759
Location: Michigan

PostPosted: Wed Mar 27, 2002 7:20 pm    Post subject: Reply with quote

Hmm, good to know. I wondered if I'd need to be corrected on that Wink
Anything else you can think of that would help optimise code that we might not be doing?

_________________
-Sheep
My pockets hurt...
Back to top
View user's profile Send private message Send e-mail
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Wed Mar 27, 2002 8:15 pm    Post subject: Reply with quote

Hmmmm... Prakash is probably right about a slight speed
difference between the two, but I think it would be hard to
measure. And with several similiar dialog elements, I'd go
for the repeat loop myself.

Also, if the speed on a repeat loop does make a difference,
this is going to be the case any time you use one. And I
certainly don't want to add 100 lines of code (or whatever)
every place I'm using a loop.

I usually go for fewer lines of code...

Cheers, Mac Smile
__________________________________________________________________________________________________________________________

_________________
VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Back to top
View user's profile Send private message Send e-mail
Tommy
Admin Team


Joined: 16 Nov 2002
Posts: 746
Location: The Netherlands

PostPosted: Thu Mar 28, 2002 3:54 pm    Post subject: Reply with quote

Just about this specific thing mentioned by Sheep, using @BOTH() instead of multiple IF statements may not always be faster as in:

SnarlingSheep wrote:

Code:

if @EQUAL(%%Blah,Blah)
  if @EQUAL(%%Blah2,Blah)
    goto Blah
  end
end


Code:

if @BOTH(@EQUAL(%%Blah,Blah),@EQUAL(%%Blah2,Blah))
  goto Blah
end



In the first case, if @EQUAL(%%Blah,Blah) is false, the next IF statement with another @EQUAL() function isn't evaluated at all, it's simply skipped. In the first example, both @EQUAL() functions are evaluated, no matter the result of the first function.

Tommy
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Henrik
Valued Newbie


Joined: 09 Jul 2000
Posts: 35
Location: Copenhagen, Denmark

PostPosted: Thu Mar 28, 2002 4:28 pm    Post subject: Reply with quote

Hi !

As far as search algorithms are concerned, any algorithm other than the linear search are very much fasater than the linear search (for instance binary search, mergesort, bubblesort, etc) and this also goes for implementations in VDS.

Also I try to use built in functions instead of doing them myself, like using the @match() function instead of iterating through the list myself to find a perfect match.

Henrik

_________________
Henrik Skov
Email: henrikskov@mail.dk
Back to top
View user's profile Send private message Send e-mail
moke
Contributor
Contributor


Joined: 02 Jan 2002
Posts: 162

PostPosted: Thu Mar 28, 2002 5:03 pm    Post subject: Reply with quote

Although I am generally amazed that I can even get code work (occasionally). I usually shoot for as few lines of code as possible. Fewer lines is not always faster but it will often be faster. In my case the more important issue is editing. Fewer lines of code makes it easier for me to understand and I need all the help I can get.
Back to top
View user's profile Send private message Send e-mail
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Mar 28, 2002 5:16 pm    Post subject: Reply with quote

Tommy brought up a good point about IF/ELSE statements.
It's a good idea to put all functions/commands that have a
common requirement under one IF statement. If there are
several functions/commands in a loop (or whatever), always
start with the broadest range first and work your way down.
This can save a lot of CPU cycles by not checking several
functions separately for the same criteria.

LOL, and I totally agree with moke... Laughing

Cheers, Mac Smile

_________________
VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Back to top
View user's profile Send private message Send e-mail
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Sat Mar 30, 2002 6:33 am    Post subject: Reply with quote

Tommy, are you sure that each procedure in a @both()
function is checked regardless of the condition of the
first one? Either one being false will return null, so why
would it check the second if the first is false? Or is this
just a DELPHI thing...

Cheers, Mac Smile

_________________
VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Back to top
View user's profile Send private message Send e-mail
Tommy
Admin Team


Joined: 16 Nov 2002
Posts: 746
Location: The Netherlands

PostPosted: Sat Mar 30, 2002 8:38 am    Post subject: Reply with quote

VDS first evaluates the functions most inside. For example in:

@BOTH(@EQUAL(1,2),@EQUAL(2,2))

it'll first evaluate @EQUAL(1,2) and @EQUAL(2,2) and only then it will apply @BOTH() to check if both are true. The evaluator must work like this:

@BOTH(@EQUAL(1,2),@EQUAL(2,2))
@BOTH([null],1)
[null]


Tommy
Back to top
View user's profile Send private message Send e-mail Visit poster's website
VDSuser
Contributor
Contributor


Joined: 21 Mar 2002
Posts: 58
Location: Somewhere in time

PostPosted: Sat Mar 30, 2002 4:24 pm    Post subject: Reply with quote

If you've ever programmed in other interpreted languages you'll realize that Tommy is correct. Any time a call is made, the language must interpret the call. Therefore, nested IF statements will execute faster because if the first statement returns FALSE, then none of the nested calls will be performed.

Keeping in mind that the key word in this discussion is "Optimize," here are some other tips:

1. Use variables for often-called statements. For example, if your code often calls @PATH() to refer to the same location, use "%p = @PATH()" one time and then use %p instead of repeating the @PATH call. As %p is already in memory, it's returned faster with less overhead on your program.

2. Don't assign one-use strings to variables, as it wastes resources. The statement "@INFO This is a line of information" is more efficient than "%m = This is a line of information | @INFO %m" if you intend to use the statement only once.

3. Recycle variable names. When %m is no longer used in your code, use %m for something else. This releases the previous value from memory. If %m contains a lot of data that will no longer be used, release it with an "%m =" statement.

4. Keep variable names short. Longer names make for larger compiled code and use more memory.

5. Use subroutines instead of repeating blocks of code. Subroutines keep your main section short and are easier to manage.

Back in the days of 640K RAM, 6Mhz processors, 20MB hard disks and 360K floppies, we programmers were forced to find ways to make sleeker code. Capacities may be much larger today but, in coding, economy still makes for better programs. Smile
Back to top
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Sat Mar 30, 2002 5:17 pm    Post subject: Reply with quote

Thanks Tommy, I shoulda remembered that from the script
interpreters we made... Rolling Eyes

_________________
VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Back to top
View user's profile Send private message Send e-mail
Serge
Professional Member
Professional Member


Joined: 04 Mar 2002
Posts: 1480
Location: Australia

PostPosted: Sat Mar 30, 2002 10:55 pm    Post subject: thanks to all Reply with quote

Thanks to all who replied to my question, i appreciate your thoughts and answers.

Does it matter where you place subroutines? My understanding is that when using subroutines and you call on one, then the program will always start from the start and search for the subroutine being called. If this is the case, then the often called subroutines would be better placed at the top of the program. Is that right?

Serge

_________________
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Sun Mar 31, 2002 1:35 am    Post subject: Reply with quote

I don't think it's gonna make that much difference. You're
not building games like QUAKE etc. where micro-seconds
add up pretty quick.

I usually put subroutines at the end. It makes everything
easier to keep up with (in my opinion anyway...).

Cheers, Mac Smile

_________________
VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group