willichan Posted August 17, 2013 Share Posted August 17, 2013 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? My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash Link to comment Share on other sites More sharing options...
czardas Posted August 17, 2013 Share Posted August 17, 2013 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. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
willichan Posted August 17, 2013 Author Share Posted August 17, 2013 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. My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash Link to comment Share on other sites More sharing options...
czardas Posted August 17, 2013 Share Posted August 17, 2013 (edited) 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 August 17, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
JohnOne Posted August 17, 2013 Share Posted August 17, 2013 I thought the only difference is that you cannot accidentally change it. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
willichan Posted August 17, 2013 Author Share Posted August 17, 2013 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) My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash Link to comment Share on other sites More sharing options...
FireFox Posted August 17, 2013 Share Posted August 17, 2013 with the Const version being the larger.Sure because "Const" takes more place than "".What's the point of doing this?Br, FireFox. Link to comment Share on other sites More sharing options...
czardas Posted August 17, 2013 Share Posted August 17, 2013 (edited) 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 August 17, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
FireFox Posted August 17, 2013 Share Posted August 17, 2013 (edited) The constants are a matter of speed and memory efficiency, though negligible. Edited August 17, 2013 by FireFox Link to comment Share on other sites More sharing options...
Solution MHz Posted August 17, 2013 Solution Share Posted August 17, 2013 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. willichan and mLipok 2 Link to comment Share on other sites More sharing options...
willichan Posted August 17, 2013 Author Share Posted August 17, 2013 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. My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now