08-29-2011 03:49 PM
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!
08-29-2011 07:11 PM
Variants are much faster because no data conversion is required. See here.
08-29-2011 09:00 PM
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)
08-29-2011 10:01 PM
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.