Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS RegEx Plugin
Last modified Fri, 5th May 2022.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /RegEx/PCRE2/PCRE2 Speed Test
Download this example: PCRE2 Speed Test.zip
Project "PCRE2 Speed Test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Const kMarker = "\rEND:VCARD\r"
Const kMarkerPattern = "(?U-i)^END:VCARD\R"
Control btnMakeString Inherits PushButton
ControlInstance btnMakeString Inherits PushButton
EventHandler Sub Action()
#Pragma DisableBackgroundTasks True
dim size as integer = fldTargetSize.Text.Val * 1024 * 1024
dim occurrences as integer = fldOccurrences.Text.Val
if size < 1 or occurrences < 1 or ( kMarker.LenB * occurrences ) > size then
AddToResult "The entered numbers are not valid."
return
end if
dim diff as integer = size - ( kMarker.LenB * occurrences )
dim diffPerOccurrence as integer = diff \ occurrences
dim s as string = "0123456789 abcdefghijklmnopqrstuvwxyz" + EndOfLine
while s.LenB < diffPerOccurrence
s = s + s
wend
s = s.LeftB( diffPerOccurrence ) + kMarker
dim targetSize as integer = s.LenB * occurrences
while s.LenB < targetSize
s = s + s
wend
s = s.LeftB( targetSize )
TestString = s
AddToResult "String Created: " + format( s.LenB, "#," ) + " bytes"
End EventHandler
End Control
Control fldResult Inherits TextArea
ControlInstance fldResult Inherits TextArea
End Control
Control btnRegExMBS Inherits PushButton
ControlInstance btnRegExMBS Inherits PushButton
EventHandler Sub Action()
#Pragma DisableBackgroundTasks True
dim msg as string
dim startTime, diffTime as double
if TestString = "" then
AddToResult "Create the string first."
return
end if
AddToResult "RegExMBS"
'AddToResult "String Length: " + format( TestString.LenB, "#," )
'AddToResult "Marker: " + kMarker
startTime = microseconds
dim rx as new RegExMBS
rx.CompileOptionCaseLess = True
rx.CompileOptionDotAll = False
rx.CompileOptionUngreedy = False
rx.CompileOptionNewLineAnyCRLF = True
rx.ExecuteOptionNotEmpty = False
rx.CompileOptionMultiline = True
rx.CompileOptionNoUTF8Check = true
rx.CompileOptionUTF8 = true
if not rx.Compile( kMarkerPattern ) then
AddToResult "Could not compile the pattern."
AddToResult rx.ErrorMessage
return
end if
if not rx.Study then
AddToResult "Study not successful."
return
end if
dim foundCount as integer
dim offset as integer = rx.Execute( TestString, 0 )
while offset > 0
foundCount = foundCount + 1
offset = rx.Execute( rx.Offset( 1 ) + 1 )
wend
diffTime = microseconds - startTime
msg = format( diffTime, "#," ) + " microseconds"
AddToResult msg
msg = format( foundCount, "#," ) + " found"
AddToResult msg
End EventHandler
End Control
Control fldOccurrences Inherits TextField
ControlInstance fldOccurrences Inherits TextField
End Control
Control Label1 Inherits Label
ControlInstance Label1(0) Inherits Label
ControlInstance Label1(1) Inherits Label
ControlInstance Label1(2) Inherits Label
ControlInstance Label1(3) Inherits Label
End Control
Control fldTargetSize Inherits TextField
ControlInstance fldTargetSize Inherits TextField
End Control
Control btnRegEx Inherits PushButton
ControlInstance btnRegEx Inherits PushButton
EventHandler Sub Action()
#Pragma DisableBackgroundTasks True
dim msg as string
dim startTime, diffTime as double
if TestString = "" then
AddToResult "Create the string first."
return
end if
AddToResult "RegEx"
'AddToResult "String Length: " + format( TestString.LenB, "#," )
'AddToResult "Marker: " + kMarker
startTime = microseconds
dim rx as new RegEx
rx.Options.CaseSensitive = False
rx.Options.DotMatchAll = False
rx.Options.Greedy = True
rx.Options.LineEndType = 0
rx.Options.MatchEmpty = true
rx.Options.TreatTargetAsOneLine = false
rx.SearchPattern = kMarkerPattern
dim foundCount as integer
dim match as RegExMatch = rx.Search( TestString )
while match <> nil
foundCount = foundCount + 1
match = rx.Search
wend
diffTime = microseconds - startTime
msg = format( diffTime, "#," ) + " microseconds"
AddToResult msg
msg = format( foundCount, "#," ) + " found"
AddToResult msg
End EventHandler
End Control
Control btnPCRE2 Inherits PushButton
ControlInstance btnPCRE2 Inherits PushButton
EventHandler Sub Action()
#Pragma DisableBackgroundTasks True
Dim msg As String
dim startTime, diffTime as double
if TestString = "" then
AddToResult "Create the string first."
return
End If
dim JIT as Boolean = Keyboard.AsyncOptionKey
if jit then
AddToResult "PCRE2 with JIT"
Else
AddToResult "PCRE2"
End If
startTime = microseconds
Dim rx As New PCRE2CompilerMBS
rx.CaseLess = True
rx.DotAll = False
rx.Ungreedy = False
rx.NewLine = rx.kNewLineAnyCRLF
rx.Multiline = True
rx.Pattern = kMarkerPattern
Dim code As PCRE2CodeMBS = rx.Compile
If JIT Then
code.JITCompile(code.kJITComplete)
if code.JITSize = 0 then
AddToResult "No JIT!"
end if
End If
'Dim matches() As PCRE2MatchDataMBS = code.MatchAll(TestString)
'Dim foundCount As Integer = matches.Ubound + 1
Dim MatchData As New PCRE2MatchDataMBS(code)
Dim foundCount As Integer
Dim Found As Integer = code.Match(TestString, MatchData, 0)
While Found > 0
foundCount = foundCount + 1
Found = code.Match(TestString, MatchData, MatchData.StartPosition+1)
wend
diffTime = microseconds - startTime
msg = Format( diffTime, "#," ) + " microseconds"
AddToResult msg
msg = format( foundCount, "#," ) + " found"
AddToResult msg
End EventHandler
End Control
Control btnPCRE3 Inherits PushButton
ControlInstance btnPCRE3 Inherits PushButton
EventHandler Sub Action()
#Pragma DisableBackgroundTasks True
Dim msg As String
dim startTime, diffTime as double
if TestString = "" then
AddToResult "Create the string first."
return
End If
dim JIT as Boolean = Keyboard.AsyncOptionKey
if jit then
AddToResult "PCRE2 with JIT"
Else
AddToResult "PCRE2"
End If
startTime = microseconds
Dim rx As New PCRE2CompilerMBS
rx.CaseLess = True
rx.DotAll = False
rx.Ungreedy = False
rx.NewLine = rx.kNewLineAnyCRLF
rx.Multiline = True
rx.Pattern = kMarkerPattern
Dim code As PCRE2CodeMBS = rx.Compile
If JIT Then
code.JITCompile(code.kJITComplete)
if code.JITSize = 0 then
AddToResult "No JIT!"
end if
End If
dim foundCount as integer = 0
for each MatchData as PCRE2MatchDataMBS in code.Matches(TestString, 0)
foundCount = foundCount + 1
next
diffTime = microseconds - startTime
msg = Format( diffTime, "#," ) + " microseconds"
AddToResult msg
msg = format( foundCount, "#," ) + " found"
AddToResult msg
End EventHandler
End Control
Sub AddToResult(msg As String)
fldResult.AppendText msg
fldResult.AppendText EndOfLine
End Sub
Property TestString As String
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project
See also:
Download this example: PCRE2 Speed Test.zip
The items on this page are in the following plugins: MBS RegEx Plugin.
