08-24-2017 02:21 AM
Hi,
I use the Match Pattern vi all the time for checking if some text appears in a string. I have found 2 different ways to do this see below.
what is the best method? is there a better way to do this?
08-24-2017 02:43 AM
08-24-2017 05:08 AM
i was thinking if one method was preferred over the other, does one have more benefits than the other in terms of speed, memory usage etc especially when used in a loop running hundreds or thousands of iterations. Or is there another vi that outperforms the one i have shown. Bear in mind that i only used the string of 'Hello World!' what if the string was thousands or hundreds of thousands of characters long.
08-24-2017 05:47 AM - edited 08-24-2017 05:49 AM
Hi Dave,
does one have more benefits than the other in terms of speed, memory usage etc
Comparing a number with zero is much easier than to compare a string of 3 or more chars for being equal. But both comparisons have minimal impact on execution time in comparison to the MatchPattern function: this one will take the most resources in the snippet!
Basic rule for such kind of optimizations: Invest time on the part of the algorithm which takes the main part of the resources (CPU time, memory). Right now you investigate a comparison taking less than 1% of the overall CPU time your VI will request…
08-24-2017 05:53 AM
Due to possible stomping, I would go with the second method since it has the potential of not creating new strings and therefore use less memory. Granted, with your example the savings will be unnoticeable. But I also like the less wiring required by using the Greater Or Equal To 0.
08-24-2017 10:16 AM
I'd prefer the 2nd approach too, largely because much of the time that I use "Match Pattern" the string I'm looking for includes regex-like "special characters." In such cases, the 1st method will always produce a False.
Tangent: I recently helped a colleague with some over-the-shoulder troubleshooting and found a mild surprise. He was using the "Match Regular Expression" node and then checked the 'index after match' output for >=0 to determine whether his string was found.
However, he was also wiring in the error input. The result was that whenever there was an incoming error, the index output was 0 while the matched string output was empty. This caused the >=0 check to declare a match.
It made sense in retrospect that in incoming error would put datatype defaults on all outputs, but until thinking it through it was pretty easy to have the gut feel that the outputs would say "did not find" (because "did not search").
Just a little alert that the index>=0 check can trip you up with the similar "Match Regular Expression" function if you feed it an input error. If you need the full regex function, you'll need to also check the output error.
-Kevin P
08-25-2017 05:55 AM
Thanks for all your comments, i just wanted to make sure i coding in the correct most efficient way.