Jump to content

Constants vs Variables


Go to solution Solved by MHz,

Recommended Posts

Ok.  This is probably a silly question, but it keeps moving from the back of my mind to the front every now and again.

In the context of an AutoIt script, what is the advantage of declaring a Const vs just declaring a variable with a value?

I understand in compiled languages that it can affect where the value is stored, and possibly the ammount of space it takes to store it.  In an interpreted script like Autoit, though, are there any advantages when it comes to performance or memory management to define values as constants over variables?

Link to comment
Share on other sites

A few runs loading 2K worth of text into a variable looks like it will be a difficult test.  Too much fluxuation with system processes, etc. to see what is happening with AutoIt.  I'll try again with larger data later.

Link to comment
Share on other sites

Compiled the following code with varying size strings from 10 characters to 1000, adding and removing the Const declaration.  The compile size difference fluxuated up and down by only 12-36 bytes, with the Const version being the larger.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Const $memeater = "[put data here]
MsgBox(0,"",$memeater)
Link to comment
Share on other sites

  • Solution

It is because a const in AutoIt is a read only variable as I was informed in the past. There is little advantage other then the Do-Not-Change-My-Value behavior.

Quote from myself from here about const.

 

A Const in AutoIt3 is a variable marked as read only and remains as a variable as read only even if compiled. A Const in compiled languages such as C++ may have Const variable declarations removed and the Const variables replaced with the literal values that were assigned to them during compilation. The compile of C++ would follow conditions so what happens to one variable may not happen to another variable. Thus C++ and similar compiled languages can go under the term of optimised compilation. Interpreted languages may not have many of the optimised behaviors that C++ may have so there is always difference to test. On the other side, interpreted languages have features though that C++ can only dream of. Using Const in C++ everywhere as possible is efficiency, in AutoIt3 it just read only variables that can hinder the script if used too much. Example of too much is that if you create many Consts then you may need even more variables to have the task done.

Link to comment
Share on other sites

It is because a const in AutoIt is a read only variable as I was informed in the past. There is little advantage other then the Do-Not-Change-My-Value behavior.

Quote from myself from here about const.

 

That is exactly the answer and info I was looking for.  Thank you.

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