From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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: 

String concatenation within a loop - Optimisation / best practices

Hopefully someone can help me with this Smiley Happy:

What optimisations could be made to the following code snippet to improve performance (reduce memory thrashing due to string concatenation etc.)?

This is mainly just for improving/learning more best practices and applying them in the future.

 

I would like to keep the tags and delimiters easy to change.

 

String concatenate inside loop.png

 

The output is an array of strings that look like this:

X:1.0000,2.0000,3.0000,4.0000,5.0000,6.0000;\n
Y:0.00,0.10,0.20,0.30,0.40,0.50;

I've scoured the internet and forums for this topic and found a post about preallocating an array of uint8, but can't find it now.

 

Thanks!



Using LV2018 32 bit

Highly recommended open source screen capture software (useful for bug reports).

https://getsharex.com/
0 Kudos
Message 1 of 5
(4,027 Views)

Unless you're dealing with huge arrays or need to do this at very high speed (in which case, you should probably rethink other parts of your code and avoid strings entirely if possible), I wouldn't worry about optimising this. However, here's a simplification that uses Array to Spreadsheet String.

String Concatenate with Array to Spreadsheet String.png

Message 2 of 5
(3,951 Views)

Why clusters?

concat strings.png

Message 3 of 5
(3,940 Views)

Thanks nathand, that's great. I completely agree - it's just nice to see what methods can be used.

Good call on the array to spreadsheet function, likely more efficient as it's primitive function. The trim looks like it may be less efficient though, as it does a pattern match before trimming?



Using LV2018 32 bit

Highly recommended open source screen capture software (useful for bug reports).

https://getsharex.com/
0 Kudos
Message 4 of 5
(3,902 Views)

Thanks Alexander, good point.

I originally used clusters as I thought it would look neater with less wires going into the loop, but yours looks just as tidy and more effiecient.



Using LV2018 32 bit

Highly recommended open source screen capture software (useful for bug reports).

https://getsharex.com/
0 Kudos
Message 5 of 5
(3,898 Views)