Jump to content

reassigning the same value to a variable repeatedly

Recommended Posts


Is this better to check a variable before you assign it to a value that could be the same?

for example:

local $EmptyLog = false

func WriteLog($text)
    _guictrledit_appendtext($log, ($EmptyLog ? @CRLF : $empty) & $text)
    If $EmptyLog Then $EmptyLog = False

or does AutoIt behind the scenes already check this?

i guess overwriting memory with the same value over and over again is not good if you can prevent this with a check?

Share this post

Link to post
Share on other sites

Generally it is cheaper to overwrite than to check first

The only time that would change is if there are a lot of calculations that need performed to come up with the value or larger datatypes (arrays etc.)

Share this post

Link to post
Share on other sites

Hi Bilgus,

so basically it's a tiny bit better to do this, but it won't change things dramatically unless you go bigger then a boolean.

i'm a bit obsessive compulsive with sparing my computer some work and it's just interesting to know how stuff works :)

Edited by TheAutomator

Share this post

Link to post
Share on other sites

In the end you just caused it extra work because it had to move things around to check the value and then decide to run or jump past run code to set the value

It ends up being less instructions for the PC to set the value blindly..

Its down to counting grains of sand in a desert and in Autoit it probably isn't going to make any difference but in C for instance you just added two or 3 instructions and that makes a noticeable difference in a really tight loop where performance counts

Really unless you have an issue with performance in the end you are probably better off just going with whatever is easiest to read

try something like this

Local $t = TimerInit()
Local $b = True
For $i = 0 to 100000
    If $b = true Then $b = False
ConsoleWrite("T1 " & timerDiff($t) & @CRLF)

Local $t = TimerInit()
For $i = 0 to 100000
    $b = False
ConsoleWrite("T2 " & timerDiff($t) & @CRLF)


  • Like 2

Share this post

Link to post
Share on other sites

yep, you are causing more work by checking, i would not agree that what you are doing is any better. just more cycles.

Edited by Earthshine

My resources are limited. You must ask the right questions


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

  • Similar Content

    • JNutt
      By JNutt
      I see $hWnd used as a local variable in a lot of script examples.  I know the 'h' is used for handles, but what is the 'Wnd' short for?
    • Rskm
      By Rskm
      i have seen many pointing out issues related to this, i tried reading through them - still unable to solve mine.. i have a script which reads certain lines based on a search item, this is an iterative process - the ldflst_file gets updated for each iteration and the script finds the line number which has the search item. this works perfectly for few iterations, but stops in some cases abruptly with the error = 
      Subscript used on non-accessible variable $aRead^ ERROR I have verified that the search item is present in the file. what could be the reason for this code to stop in certain cases. thanks for help
      my code below (partial).. full code is in attachment
      local $aRead=FileReadToArray($ldflst_file)
      global $ldflst_var1=" JOINT  LOAD       FORCE(X)        FORCE(Y)        FORCE(Z)       MOMENT(X)       MOMENT(Y)       MOMENT(Z)"
      for $i= 0 to $ldflst_totlines    ; ldflst_totlines is total number of  lines in the file 'ldflst_file'

          if $aRead[$i]=$ldflst_var1 Then
    • kartune
      By kartune
      Hello, i'm trying to rename a variable to another variable name (not the value attached to the variable).  This would save me TONS of coding.
      Here is an example:
      Local $clear1="$SATJ"  ;sets the $clear1 variable to become $SATJ
      GUICtrlSetState($clear1 & "3", $GUI_UNCHECKED)  ;i want this to come out to be $SATJ3, it should look something like - GUICtrlSetState($SATJ3, $GUI_UNCHECKED)
      Is this possible?  I've been searching all the forums and help files for hours.  My javascript friend referenced me to the solution on JS, which lead me to searching through the "assign" help on autoit, but not much info on it.
      I've already tried
      Assign($clear1, $SATJ)
      any help is greatly appreciated!!! 
    • xEviiLx
      By xEviiLx
      I'm trying to read value of a base pointer + offset.
      With only address I can easily the value but with base addres (pointer) I really don't know how I can do that.
    • Atoxis
      By Atoxis
      Howdy, this is my first post, massive fan of autoit. 
      I've searched and tried and I would just like people who are better at this than me to let me know if this is even a thing.

      I'd like to perform just a variable. For example, it would be. *see inserted code*
      So what i'm wanting is, create the constant $test, and that variable would be what is followed after the = . Then perform the _FileCreate. Then perform the variable.  Logically or in my head rather.. That variable is declared and is equal to what it is set to above, therefore just placing the variable plainly in the script, it should be equal to what it was declared as.  So what am I doing wrong, and or how can I have autoit just perform the variable.  

      #include <File.au3> Const $test = FileWriteLine(@DesktopDir & "\Log.txt", @CRLF ) _FileCreate(@DesktopDir & "\Log.txt") $test  

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.