Burgaud Posted April 27, 2021 Share Posted April 27, 2021 Assume I have an indefinite loop like so: local $NOW while true $NOW = stringformat("%02d:%02d:%02d",@HOUR,@MIN,@SEC) . . wend vs while true local $NOW = stringformat("%02d:%02d:%02d",@HOUR,@MIN,@SEC) . . wend I understand that the 1st version gives "better performance" as the interpreter does not need to allocate/create $NOW each and every time it iterates. However, I find 2nd version more manageable in terms of maintenance. ie, I can move the loop elsewhere and everything It needs are self contained. Likewise, it will not get modified elsewhere because it is localized within the loop. My questions are 1. How significant is the performance degradation of defining variables within an infinite loop? 2. What is the best practice? Thx! Link to comment Share on other sites More sharing options...
Musashi Posted April 27, 2021 Share Posted April 27, 2021 Try to run this : Example_1() Example_2() Func Example_1() Local $iCounter = 1 While $iCounter < 10 ConsoleWrite("Example_1 : Value of Counter is " & $iCounter & @CRLF) $iCounter += 1 WEnd EndFunc Func Example_2() While $iCounter < 10 Local $iCounter = 1 ConsoleWrite("Example_2 : Value of Counter is " & $iCounter & @CRLF) $iCounter += 1 WEnd EndFunc "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
Burgaud Posted April 28, 2021 Author Share Posted April 28, 2021 (edited) the 2nd example will not execute as the variable is not defined when while $iCounter < 10 was executed. It will not terminate as that variable is always kept at 1,2 ... 1,2 ... 1,2 indefinitely. PS: My question was more on defining temporary variables inside or outside of a loop, but not as the loop's condition. Old school languages require variables to be defined at the start. however, I find that very difficult to maintain afterwards. Edited April 28, 2021 by Burgaud Link to comment Share on other sites More sharing options...
JockoDundee Posted April 28, 2021 Share Posted April 28, 2021 4 hours ago, Burgaud said: the 2nd example will not execute as the variable is not defined when while $iCounter < 10 was executed. yes, it makes sense why this errors: If $a Then ConsoleWrite("error") because $a has not been assigned a value therefore this works: $a=True If $a Then ConsoleWrite("error") although this works as well, without an explicit assignment Local $a If $a Then ConsoleWrite("error") which is a little odd. Then you say, "Oh but it does make sense because if no init value is given, the statement Local $a is assigns the default of 0" but then you have the case: $a=True Local $a If $a Then ConsoleWrite("error") ??? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
argumentum Posted April 28, 2021 Share Posted April 28, 2021 On 4/27/2021 at 12:38 AM, Burgaud said: However, I find 2nd version more manageable in terms of maintenance. You know how to properly do it. If you believe that the "2nd version more manageable", ...we can have a chat in a few years maintaining disorganized code and see how this 2nd approach worked out. 5 hours ago, Burgaud said: Old school languages require... New school is the same. Might as well not declare a thing. You don't have to declare a thing in AutoIt. You'll get to repent in due time. Experience will teach you. ( we need a drop mic. emoji )🎤 Musashi 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. 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