gil900

Question about Local vs Local Static declaration in function that run repeatedly

5 posts in this topic

#1 ·  Posted (edited)

Hello,

I have question about what is better to do.

People say here that it not recommended to declare Local variables inside loop.

In my case I use function that I register with  AdlibRegister.

So what inside the function is run again and again (like in loop).

In that function I use Local variables in order to perform some internal tasks.

 

The question is - is it better to use almost always Local Static because the code in the function run again and again(like inside normal loop) ?

Because I think that in this way the  machine does not need to declare the variables again and again.

Maybe it makes the code to be faster  because it declares the variables once.

 

I think I need to use almost always Local Static in that case.

 

What do you think?

If that does not belong here then please move it
to developer chat.

 

Thank you

Edited by gil900

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

2 questions:

a] how is the AdLibRegister's time?

b] you can use a global variable to do the same work of static variable?

Edited by Detefon

Visit my repository

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

A Local Static variable is only created on the first call to the function. It works like a Global variable but the scope is limited to the function where it is created.

So if you need to access a value of a previous run of the function (like a counter) Local Static is fine. If the variable can be dropped when the function is terminated then I would use Local.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

2 questions:

a] how is the AdLibRegister's time?

b] you can use a global variable to do the same work of static variable?

 

a] 5000ms

b] Yes but I don't want to because I'd rather avoid global vars that used only by this function

 

A Local Static variable is only created on the first call to the function. It works like a Global variable but the scope is limited to the function where it is created.

So if you need to access a value of a previous run of the function (like a counter) Local Static is fine. If the variable can be dropped when the function is terminated then I would use Local.

 

One reason I know why I need to use Local Static is when (a)+(b ) is valid: (a) the variable need to be saved after the function ends. (b ) when the variable used only by this function.

I do not have questions about one reason.

 

But, I have a question about the second reason that I wrote about it here, which is to avoid from re-declare the variable again and again (In function that run again and again like code in loop).

And this is my question..

Edited by gil900

Share this post


Link to post
Share on other sites

Have you even bothered to test the two ways? Do some timing tests to see if it makes any difference and how much, I'm guessing you won't notice much of a difference at all.

Your response to water doesn't make much sense. Are you saying the value in the variable needs to be retained from one run to another? If so then Static is just what you need. If you don't need the value in the variable to be retained then don't use Static because it only gets reset once. Seems simple enough to me.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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