LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

best method to use match pattern vi

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? 

labview.JPG

 

 

0 Kudos
Message 1 of 7
(3,668 Views)

Hi Dave,

 

please define "best method"!

What are your metrics when choosing a better method?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 7
(3,661 Views)

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.      

0 Kudos
Message 3 of 7
(3,635 Views)

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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 7
(3,625 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 7
(3,618 Views)

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

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 6 of 7
(3,592 Views)

Thanks for all your comments,  i just wanted to make sure i coding in the correct most efficient way.

0 Kudos
Message 7 of 7
(3,565 Views)