Jump to content

Global variables


notta
 Share

Recommended Posts

I have to use @computername quite a few times in a tool that I use. Since I have to use it so often would it be more efficient to set a global variable like $computername = @computername or just use @computername each time I need it? I was thinking since you have to read the registry each time to get what @computername was equal to I thought it would be quicker to just set it once.

Also, I have some input fields that I need to read data from quite a few times as well. Would it be more efficient to set each field to a variable or GuiCtrlRead the inputs each time I need them?

I only ask this because I have a large amount of global variables and I'm trying to clean this script up. Thanks.

Link to comment
Share on other sites

This little demo shows that if you are going to be using @ComputerName a lot saving the value in a global variable is about 100 time faster. However in practical terms the saving is minimal as @ComputerName quite fast. As a general rule it is best to call a function or calculate a value once and store the value in a variable if the result is going to be the same. The best thing to do is experiment like I did and find out what makes a real difference.

Whilst lots of global variables is considered to be a bad thing if used carefully they can improve your script if used for values that once calculated will not change very often and the value is used in many different functions.

Dim $n = ''
Dim $c = ''
Dim $T = TimerInit()

For $i = 1 To 1000
    $n = @ComputerName
Next
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : TimerDiff($T) = ' & TimerDiff($T) & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console


Dim $T = TimerInit()
$c = @ComputerName
For $i = 1 To 999
    $n = $c
Next
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : TimerDiff($T) = ' & TimerDiff($T) & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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