Jump to content
Sign in to follow this  
willichan

Constants vs Variables

Recommended Posts

willichan

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?

Share this post


Link to post
Share on other sites
czardas

Why not try some tests using MemGetStats(). Declare some massive variables and see if you can determine anything from that. I'd be interested to hear if you could determine a difference.

Share this post


Link to post
Share on other sites
willichan

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.

Share this post


Link to post
Share on other sites
czardas

I guessed it might be difficult to tell a difference. If anything I would expect a constant (being a simpler case) to have less overhead. Perhaps that's a wildly incorrect assumption on my part.

Edited by czardas

Share this post


Link to post
Share on other sites
willichan

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)

Share this post


Link to post
Share on other sites
FireFox

with the Const version being the larger.

Sure because "Const" takes more place than "".

What's the point of doing this?

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
czardas

Perhaps there is no difference, but I think there could be subtle differences between languages. I don't quite understand how memory is allocated - that's something that might make a difference.

Edited by czardas

Share this post


Link to post
Share on other sites
FireFox

The constants are a matter of speed and memory efficiency, though negligible.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
MHz

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.

  • Like 2

Share this post


Link to post
Share on other sites
willichan

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.

Share this post


Link to post
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
Sign in to follow this  

×