LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Darren's Weekly Nugget 10/09/2006

The following tip has been on my list of possible nuggets for a while, but since it has recently been discussed on LAVA, I figured I should go ahead and mention it before it becomes old news.  🙂  It's basically a faster way to accomplish the following:

You see here that we have a data structure where objects are identified by their names.  We could probably make this code easier to read (and possibly faster) by maintaining a separate array of the names and simply searching that array with Search 1D Array:

Both of these methods are inherently linear searches.  It turns out that the following method of accessing the data is much faster:

I had never given much thought to Variant Attributes until this trick was shown to me.  You can basically store each data object as a different attribute of some dummy variant.  I say it's a "dummy" variant because its actual value and datatype are inconsequential...we're purely using the variant to gain access to its attributes as a data store.  I'm no computer scientist, but apparently the variant attributes are stored using a "red/black tree" algorithm, which ends up being an order log(n) search...this is much faster than an order n search, which is what the While Loop or Search 1D Array approach would be.

So the next time you find yourself creating a string array lookup similar to one of the two traditional approaches above, try giving the Variant Attribute approach a try.  Note that this approach is recommended for LabVIEW 8.0 and later, due to some performance improvements with variants that did not exist in LabVIEW 7.1 and previous.

-D

P.S. - Check out past nuggets here.

Message Edited by Darren on 10-09-2006 11:14 AM

Download All
Message 1 of 25
(32,408 Views)

Interesting, but unless I missed something (entirely possible, mind you) a few details remain to be explained.  Does this assume that the Object Data cluster contains a string?  If so, does said string have to be at the "top-level" of the cluster?  If there are multiple strings in Object Data, does it use the first one in the cluster order, the highest-level one, etc?

I also don't quite understand, the first two find an item with a particular name in an array of clusters.  The variant approach looks like it is doesn't have any arrays in it, so what is it searching through?

Message Edited by Jeff B on 10-09-2006 11:27 AM

0 Kudos
Message 2 of 25
(32,396 Views)

Object Data can contain whatever you want...it's just that whenever you add the object data to the variant attributes, you're associating a specific name (the attribute name) with that data for looking up later.  You can check out the LAVA discussion I linked above for more details if it's still unclear.

-D

0 Kudos
Message 3 of 25
(32,391 Views)

Is there anyone out there who would like to translate this nugget for the "variant diabled" (like me!) ?

Given the "Old way" what would I have to do to convert my code to the new way?

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 4 of 25
(32,382 Views)
I'm attaching a simple example that should help a little. The best way I can phrase this is to think of the dummy variant Darren described more as a refnum to some storage. The refnum itself is meaningless, but it allows us to call functions (like Get/Set) on that specific storage in memory.

So we can store any type of data object as a name/value pair inside the variant and then retrieve those values by name. You pass around the variant data like you would a refnum. The big difference in this analogy is that if you copy the variant, you copy the entire storage, which is not what happens with a refnum or any other type of reference.

Message Edited by Jarrod S. on 10-09-2006 01:10 PM

Jarrod S.
National Instruments
Message 5 of 25
(32,358 Views)