Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15079

LIKE Operator speed-up without additional references

$
0
0
Hello all,

is there any faster way to compare multiple strings via regex-patterns than I did in the example code below?
This code works very well, but becomes slow when executed multiple times in a for-loop
(tested with QueryPerformanceCounter-/QueryPerformanceFrequency-API).
Please note that my intention is to avoid additional references to runtime libraries
(e.g. Microsoft Scripting Runtime) and/or API-declarations as much as possible.

Do I have to rely on this code or are there any speed improvements around despite the selfimposed limitations above?

Any suggestion is greatly appreciated.


Zphere


Example bas-module:
Code:

Option Explicit
Private Const REGEX_PATTERN1 As String = "REGEX1|REGEX2|REGEX3"
Private Const REGEX_PATTERN2 As String = "REGEX4|REGEX5|REGEX6|REGEX7|REGEX8|REGEX9|REGEX10|REGEX11|REGEX12"
Private Const REGEX_PATTERN3 As String = "REGEX13|REGEX2|REGEX14"

Public Function LikeEx(ByRef vValue As Variant, sArg As String, Optional Delim As String = "|") As Boolean
' Like-Operator - supports multiple arguments by delimiter (pipe as default)
  Dim X As Long, sArr() As String
  sArr = Split(sArg, Delim)
  For X = LBound(sArr) To UBound(sArr)
    If sArr(X) <> vbNullstring Then
      If vValue Like sArr(X) Then LikeEx = True: Exit For ' artificial OR
    End If
  Next
  Erase sArr
End Function

Private Sub Test(ArrIn() as variant)
  For I = LBound(ArrIN, 2) To UBound(ArrIN, 2) 
    Select Case True

      Case LikeEx(ArrIn(0,I, REGEX_PATTERN1)
          LikeEx(ArrIn(0,I, REGEX_PATTERN2)
          Do something

      Case LikeEx(ArrIn(0,I, REGEX_PATTERN2)
          LikeEx(ArrIn(0,I, REGEX_PATTERN3)
          Do something

      Case LikeEx...
      Case LikeEx...
      Case LikeEx...
    End Select
  Next
End Sub

Edit: changed sNULL to vbNullstring

Viewing all articles
Browse latest Browse all 15079

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>