Jump to content

Global variable solution


Recommended Posts

We have been doing some optimizing of some existing monster code and we have found that instances of global variables is a problem. Specifically, we have two global variable vi's that contain ~ 150 variables each.

In the heirarchy there are approximately 600 instances strewn about.

I have found a solution that may work for us, but I wanted to get some further opinions.

Basically the solution is this: At the top level, we have one instance of one of the global variables just to ensure that the vi is in the heirarchy and in memory. We then use the variable name to access the data using property nodes passing the data in(or out) as variant. Since there are only a few data types, we then use a polymorphic vi to essentially convert the data to (and from) variant.

I have an example attached.

What do you guys think? Will this help my memory size? We have found that this method is the closest to a drop in replacement for global variables.

Thanks.

In the example, look at "Global example.vi" and "Old way global example.vi"

Download File:post-1336-1105663280.zip

Link to comment
We have been doing some optimizing of some existing monster code and we have found that instances of global variables is a problem.  Specifically, we have two global variable vi's that contain ~ 150 variables each.

In the heirarchy there are approximately 600 instances strewn about.

3509[/snapback]

Seems you are in the same boat as me :( . This programming technique is traditionally called "C code programmed with LabVIEW" - we have several programs in our company written this way... how funny to debug them :blink:

Basically the solution is this:  At the top level, we have one instance of one of the global variables just to ensure that the vi is in the heirarchy and in memory.  We then use the variable name to access the data using property nodes passing the data in(or out) as variant.  Since there are only a few data types, we then use a polymorphic vi to essentially convert the data to (and from) variant.

3509[/snapback]

Your Idea is good, but I suspect that replacing the global variable (in the code) with a property node would be helpful. You better replace the global with LV2-global.

But this would be only the first step. You still don't know when and where a variable is read/modified. Sooner or later it would be easier to look at what the program ought to do and then rewrite the whole bunch by scratch! You could also begin by replacing module by module with e.g. openGoop or so.

Didier

Link to comment

Taking Programs that work and improving the code now has an offical name called refactoring.

LTR magazine has an article this quarter that talks about this topic.

I have also inhearted large programs with globals every where. I used the LV type 2 global method to handel the problem.

I added my method to you example code so you can compare.

Because I'm a poor speller\typist I prefer to put all the variable name in an Enum type def and select the name from the list.

You mentioned that there were about 600 instanses throuout the program. In my mind that means 600 potential mispelled\mistyped bugs.

Download File:post-584-1106109069.llb

Link to comment
We have been doing some optimizing of some existing monster code and we have found that instances of global variables is a problem.  Specifically, we have two global variable vi's that contain ~ 150 variables each.

In the heirarchy there are approximately 600 instances strewn about.

I have found a solution that may work for us, but I wanted to get some further opinions.

Basically the solution is this:  At the top level, we have one instance of one of the global variables just to ensure that the vi is in the heirarchy and in memory.  We then use the variable name to access the data using property nodes passing the data in(or out) as variant.  Since there are only a few data types, we then use a polymorphic vi to essentially convert the data to (and from) variant.

I have an example attached.

What do you guys think?  Will this help my memory size?  We have found that this method is the closest to a drop in replacement for global variables.

Thanks.

In the example, look at "Global example.vi" and "Old way global example.vi"

Download File:post-1336-1105663280.zip

3509[/snapback]

Rule number one in my company is:

1. No Global variables

Rule number two is:

2. Read rule number one.

Anyone caught breaking this rule is reprimanded. Forget globals, remove it from your palette (if you can). The solution you provide is worse! You know how slow your read and writes are gonna be? oh man! Open a reference to the global vi then using property nodes, wholy cow! Very very slow. especially if you have 600 of them. Very bad programming style.

Why re-invent the wheel?

Read this:

What are USR Globals?

Spend a day or two and rewrite the global calls to USR globals it's well worth it. Do it behind your bosses back. The program will run better and faster and you will be a hero.

Also, if you feel like reading, here:

http://forums.lavausergroup.org/index.php?showtopic=652

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.