Jump to content

Some technical quesitons about coding in Autoit.


Recommended Posts

Greetings to all,

I'm rather new to coding and have a couple of technical questions about how to write better scripts. If you can share your opinions and knowledge I'll be very glad.

1) I've read in several threads that it is wise to delete an variable if it is not needed anymore. So I always try to do so in my scripts. Like:

Func SomeFunction($i_hwnd,$i_height, $i_animationspeed=25)
    $aiWinPos = WinGetPos($i_hwnd)
    $aiWinClientSize = WinGetClientSize($i_hwnd)
;Script does couple of things with the variables...
;Finaly, just before the function exits:
    $aiWinClientSize=0
    $aiWinPos = 0
    $i_hwnd = 0
    $i_animationspeed = 0
Return 1
EndFunc

Does this help my script to use less ram and/or work faster? Or just inflates my script with unneeded lines and is a waste of time?

Ans: Local variables are destroyed when the function they are created in returns so setting them to zero is wasting time from a programming and an execution point of view. (martin)

If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.(Autoit Help File)

2) Which is better when working with binary (E.g: a jpg image file)

Func Image_FileBin()
    $Image_FileBin= "0x89504E470D0A1A0A000..."
    Return  Binary($Image_FileBin)
Endfunc

;or just 

Global $Image_FileBin= "0x89504E470D0A1A0A000..." at the beginning of the script.

I have also noticed that if I use "Global $Image_FileBin= '0x89504E470D0A1A0A000...'" and call IsBinary($Image_FileBin), it returns 0. Although it seems $Image_FileBin and Binary($Image_FileBin) are exactly the same when I print them to Scite's console. (As you can understand IsBinary(Binary($Image_FileBin) returns 1 :huh2:)

Ans: The quotes at the beginning and the end of binary code makes binary code a string. (AdmiralAlkex)

3) Is it better to work with 20 different variables or with a single array that has 20 elements.(It, most likely, is easier to work with an array (with for $i=...) but my question is there a difference in performance or memory consumption that is worth the trouble?)

Ans: Prefer arrays when it is easier to work with. (martin)

For more detailed info see LaCastiglione's reponse below. Direct Link:

4) Are there any limits for an arrays size that is needed to be taken in consideration?

Ans: A maximum of 64 dimensions and/or a total of 16 million elements (Autoit help file)

5) Do predefined variables (e.g. $GUI_HIDE and $WS_POPUP) change from one OS version to another or is it safe to use 0x80000000 in place of $WS_POPUP.

Ans: They are not intended to be change frequently so yes it is rather safe to use real values in place of variables. (martin)

6) If a scripter forgets to call DllClose, FileClose, InetGet, _IECreateEmbeded etc. does Autoit automatically closes them on exit (even in a crash)?

Windows takes care of them. (AdmiralAlkex)

7) I've noticed compiled autoit scipts are bigger in size compared to most of the other programming languages. I've read that this is because autoit is a "high-level" programming language (which is also why it is easier to code in autoit compared to those programming languages) but are there some tips, that you can share, to make autoit executables a little smaller. (Before compressing them with UPX ;)).

Use the obfuscator with parameters "/sf /sv /om /cs=0 /cn=0". You'll get smaller size AND faster speed. See SciTE help for how all this work.(AdmiralAlkex)

8) Are there any common performance bottle necks, to be avoided, that you can think of? (e.g. using Switch and Case's instead of ElseIf's when possible)

Remark: Using Switch and Case's instead of ElseIf's has nothing to do with performance, that is just for readability (AdmiralAlkex)

Thanks in advance to everybody for their input.

Regards

Tip

Edit: I will try to update this post with corresponding answers as I get them... I hope it helps others with same questions in mind :alien:

Edited by tip

[center]MsgBox_Tipped: Eye candy msgboxes/inputboxes/loginboxes. | CreateBlankBox: Semi-transparent layers with borders and rounded corners.[/center]

Link to comment
Share on other sites

Greetings to all,

I'm rather new to coding and have a couple of technical questions about how to write better scripts. If you can share your opinions and knowledge I'll be very glad.

1) I've read in several threads that it is wise to delete an variable if it is not needed anymore. So I always try to do so in my scripts. Like:

Func SomeFunction($i_hwnd,$i_height, $i_animationspeed=25)
    $aiWinPos = WinGetPos($i_hwnd)
    $aiWinClientSize = WinGetClientSize($i_hwnd)
//Script does couple of things with the variables...

//Finaly, just before the function exits:
    $aiWinClientSize=0
    $aiWinPos = 0
    $i_hwnd = 0
    $i_animationspeed = 0
Return 1
EndFunc

Does this help my script to use less ram and/or work faster? Or just inflates my script with unneeded lines and is a waste of time?

2) Which is better when working with binary (E.g: a jpg image file)

Func Image_FileBin()
    $Image_FileBin= "0x89504E470D0A1A0A000..."
    Return  Binary($Image_FileBin)
Endfunc

or just 

Global $Image_FileBin= "0x89504E470D0A1A0A000..." at the beginning of the script.

I have also noticed that if I use "Global $Image_FileBin= '0x89504E470D0A1A0A000...'" and call IsBinary($Image_FileBin), it returns 0. Although it seems $Image_FileBin and Binary($Image_FileBin) are exactly the same. (As you can understand IsBinary(Binary($Image_FileBin) returns 1 :huh2:)

3) Is it better to work with 20 different variables or with a single array that has 20 elements.(It, most likely, is easier to work with an array (with for $i=...) but my question is there a difference in performance or memory consumption that is worth the trouble?)

4) Are there any limits for an arrays size that is needed to be taken in consideration?

5) Do predefined variables (e.g. $GUI_HIDE and $WS_POPUP) change from one OS version to another or is it safe to use 0x80000000 in place of $WS_POPUP.

6) If a scripter forgets to call DllClose, FileClose, InetGet, _IECreateEmbeded etc. does Autoit automatically closes them on exit (even in a crash)?

7) I've noticed compiled autoit scipts are bigger in size compared to most of the other programming languages. I've read that this is because autoit is a "high-level" programming language (which is also why it is easier to code in autoit compared to those programming languages) but are there some tips, that you can share, to make autoit executables a little smaller. (Before compressing them with UPX ;)).

8) Are there any common performance bottle necks, to be avoided, that you can think of? (e.g. using Switch and Case's instead of ElseIf's when possible)

Thanks in advance to everybody for their input.

Regards

Tip

1) Local variables are destroyed when the function they are created in returns so setting them to zero is wasting time from a programming and an execution point of view.

2) $Image_FileBin and Binary($Image_FileBin) are NOT exactly the same and you need to read your own post to see that. In your example you set $Image_FileBin to a string; Binary returns a binary number. If you use AutoIt to print them out then you might think that they are the same because AutoIt converts them both to printable strings as needed. But try IsString(Binary("0x34")).

3)If you can use arrays and it saves code then do is my preference, but there are no rules. If arrays save repeating code then that is a very good reason to use them. Write as little as possible.(Could be applied to posts as well.) But don't hold back on the comments to help you understand the code you wrote an hour ago.

4) read the help, see section Appendix|limits/defaults.

5) Global constants like $WS_POPUP are due to be changed to much smaller numbers when the Milky Way disappears into the mother of all black holes but not before.

6) I don't know but assume you should close them.

7) I've lost the will to go on...................

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Well, here's some of my opinions.

  • Do you understand how variable scopes work? Open helpfile, read Language Reference > Variables > Scope. Now when you know that the variables will be destroyed anyway when the function ends, stop inflating your line count to save a handful of bytes right before it's all cleaned up anyway.

    If the data is huge and it's not about to go away, then go ahead and clean it up.

  • Binary doesn't care if it's global or local. You do what you want. And you want to avoid global if it's practical.

    I have also noticed that if I use "Global $Image_FileBin= '0x89504E470D0A1A0A000...'" and call IsBinary($Image_FileBin)

    The quotes make it a string. See Language Reference > Datatypes
  • Do what makes sense. If it's 20 different things, use 20 variables. If it's a list of 20 names, use 1 array. Makes the script more readable and easier to code.
  • Helpfile > FAQ

    Arrays: A maximum of 64 dimensions and/or a total of 16 million elements

  • They don't change, but keep in mind that readability will suffer if there's a bunch of non-explained magic numbers everywhere.

    Don't overdo it, and maybe put some comments that explains what happens and it won't be too bad.

  • I don't think a crashed process can do much of anything. Windows on the other hand deletes handles and stuff
  • It's because AutoIt is interpreted. A compiled script is just the script glued to a stub of the exe.

    To decrease the size, install SciTE4AutoIt3 if you haven't. With the full SciTE there comes lots of awesome tools to make full use of AutoIt, for example Obfuscator.

    Activate it and try the parameters:

    #Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0

    And you'll get smaller size AND faster speed. See SciTE help for how all this work.
  • 8) Are there any common performance bottle necks, to be avoided, that you can think of?

    Just the usual.

    (e.g. using Switch and Case's instead of ElseIf's when possible)

    That's for readability.
Link to comment
Share on other sites

Well, here's some of my opinions.

  • Do you understand how variable scopes work? Open helpfile, read Language Reference > Variables > Scope. Now when you know that the variables will be destroyed anyway when the function ends, stop inflating your line count to save a handful of bytes right before it's all cleaned up anyway.

    If the data is huge and it's not about to go away, then go ahead and clean it up.

  • Binary doesn't care if it's global or local. You do what you want. And you want to avoid global if it's practical.

    The quotes make it a string. See Language Reference > Datatypes

  • Do what makes sense. If it's 20 different things, use 20 variables. If it's a list of 20 names, use 1 array. Makes the script more readable and easier to code.
  • Helpfile > FAQ

  • They don't change, but keep in mind that readability will suffer if there's a bunch of non-explained magic numbers everywhere.

    Don't overdo it, and maybe put some comments that explains what happens and it won't be too bad.

  • I don't think a crashed process can do much of anything. Windows on the other hand deletes handles and stuff
  • It's because AutoIt is interpreted. A compiled script is just the script glued to a stub of the exe.

    To decrease the size, install SciTE4AutoIt3 if you haven't. With the full SciTE there comes lots of awesome tools to make full use of AutoIt, for example Obfuscator.

    Activate it and try the parameters:

    And you'll get smaller size AND faster speed. See SciTE help for how all this work.

  • Just the usual.

    That's for readability.

AdmiralAlkex thanks for the info.

About obfuscator, it really helps creating smaller executables(more than 125kb smaller). Too bad it generates error when guiregistermsg, guisetonevent, guictrlsetonevent, adlibenable etc. exist. I use them very often :huh2:...

Regards

Tip

Edited by tip

[center]MsgBox_Tipped: Eye candy msgboxes/inputboxes/loginboxes. | CreateBlankBox: Semi-transparent layers with borders and rounded corners.[/center]

Link to comment
Share on other sites

"but my question is there a difference in performance or memory consumption that is worth the trouble?)"

Arrays take up no extra memory. The nodes are guaranteed to be laid out in contiguous memory and therefore pointer arithmetic can be used to access each node. Of course this is done behind the scenes.

Let's assume that you have an array of ten elements each stores a number 1 through 10.

|1| |2| |3| |4| |5| |6| |7| |8| |9| |10|

Each one of these elements has a location in memory called an address. Let's assume for simplicity that the first element has an address 0x1. In order to access the second element the interpreter will simply add one to that address.

Link to comment
Share on other sites

AdmiralAlkex thanks for the info.

About obfuscator, it really helps creating smaller executables(more than 125kb smaller). Too bad it generates error when guiregistermsg, guisetonevent, guictrlsetonevent, adlibenable etc. exist. I use them very often :huh2:...

Regards

Tip

None of those functions generate errors in Obfuscator by default unless there's a problem with how you're using them. I use all of them in various programs I've written and all of them I have no problem using obfuscator on.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...