From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Flatten to string vs. Convert to variant

Does anyone have any speed/performance comparison between flattening a cluster to a string vs. converting to a variant?  I was trying some tests but I think the optimizing compiler in LV is not telling me the whole story...   Thanks!

0 Kudos
Message 1 of 4
(3,512 Views)

Variants are much faster because no data conversion is required.  See here.

0 Kudos
Message 2 of 4
(3,497 Views)

Thanks for that link.  I can't believe it, but it does seem that converting clusters in/out of variants is faster than using flatten/unflatten from string.  I did figure out a way to test this that I am fairly certain is not being optimized out and attached if you are interested.  The variant method is about 3x faster than the flatten to string method.  Whod-a-thunk-it!

-cb  (LV2011)

0 Kudos
Message 3 of 4
(3,487 Views)

Not sure why this is so surprising.  Flattening to a string requires making a copy of the entire data structure and rearranging it so everything in it is stored sequentially in a single block of memory.  Converting to a variant just requires allocating a pointer to the existing memory location, along with a tag that says what sort of data it contains.

 

Going the other direction, unflattening from a string requires splitting up the string at the right points and copying each element into a new structure.  To get data back from a variant you just point at the original data and as long as the types are compatible you're all set.

 

The 3x difference isn't really meaningful; the difference in speed will be proportional to the complexity of the structure.  I'd guess that if you flatten a string to a string versus converting it to a variant you'll get relatively little difference, whereas a cluster containing several large arrays will require much more time.

Message 4 of 4
(3,482 Views)