Using the profiler I’ve identified some slow parts of my code I’d like to optimize.
This is a pretty basic function. it splits a string into sections and returns the second word.
How can I make this faster?
[code]Private Function GetSecondWord(s as String) As String
dim elements(-1) as string
dim ub as integer
dim tp as string
elements = s.Split(" ")
ub = elements.Ubound
if ub > 2 then
tp = elements(2)
else
tp = “?”
end if
return tp
End Function[/code]
[quote=345454:@Brian O’Brien].This is a pretty basic function. it splits a string into sections and returns the second word.
tp = elements(2)
[/quote]
You’re sure? Seems to me it returns the THIRD word.
This regular expression will do the same thing and might be faster. Try something like this:
static rx as RegEx
if rx is nil then
rx = new RegEx
rx.SearchPattern = "^\\w+\\x20\\K\\w+"
end if
dim match as RegExMatch = rx.Search( s )
if match isa RegExMatch then
return match.SubExpressionString( 0 )
else
return "?"
end if
Hi Kem!
The first column is the method name, the second is how many times it’s called the third row is total ms in method and the 4th is the average length it takes to complete… Right out of the profiler.
I’m parsing a log file and extracting bits and pieces from a string that I need to build an object.
Looking at this message it looks like a JSON object to me. I’m wondering…
From here I need to extract id = n as = xxx and a list of ts = yyy
PresentationContext[id = 1, as = 1.2.840.10008.5.1.4.1.2.1.1 - Patient Root Query/Retrieve Information Model - FIND
ts = 1.2.840.10008.1.2.1 - Explicit VR Little Endian
ts = 1.2.840.10008.1.2 - Implicit VR Little Endian
]
[code]Sub Constructor(logLines() as String, byref currentLine as Integer)
dim l, ts as string
dim le, ri as integer
l = logLines(currentLine)
if l.InStr(“PresentationContext[”) <> 0 then
le = l.InStr(" = ") + 3 // id is just beside the first =
ri = l.InStr(",") -1 // id is just before the first ,
id = l.Mid(le, ri-le+1).CLong // id is between these two.
le = l.InStr(" - ") + 3
AbstractSyntax = l.Mid(le)
currentLine = currentLine + 1
while currentLine < logLines.Ubound
l = logLines(currentLine)
if l.InStr(" ]") <> 0 then
return
else
le = l.InStr(" - ") + 3
ts = l.Mid(le)
TransferSyntaxList.Append(ts)
end if
currentLine = currentLine + 1
wend
[quote=345509:@Norman Palardy]NthField is fastest
Thats curious - not what I would have expected[/quote]
I was surprised as well when I found that the NthFieldB was faster than Split. I had previously changed from NthFieldB to Split, but in the newer Xojo, my numbers for NtheFieldB average a 2x - 3x improvement as Brian discovered.