Optimizing code...

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]

perhaps the NTHFIELD function

If the parsing ALWAYS looks for a space:

tp = NthFieldB(s, " ", 2) If tp = "" Then tp = "?"

there’s 2 aspects to “slow”

  1. the function is called infrequently and is very slow to operate each time its called (the AVERAGE time is long)
  2. the function is called a LOT and each call is relatively fast but the volume of calls results in a long TOTAL time

case #1 is worth spending time optimizing that specific function to run faster

case #2 you need to examine WHY you cal ll this so much and try & reduce the number of calls to it

[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

(“Same thing” means what you posted in your description, not the code, as Markus underscored.)

Quite Right… 3rd word not second.

Before:
WebPageMain.GetThirdWord 2709 40.0000 0.0148

Nth Field:
WebPageMain.GetThirdWord 2709 19.0000 0.0070

Regex:
WebPageMain.GetThirdWord 2709 54.0000 0.0199

Sorry, what do those numbers mean?

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.

NthField is fastest
Thats curious - not what I would have expected
Regex is slowest
Go figure

Would anyone object me posting one other loop that ‘might’ be better implemented?

uh why would anyone object ?

I OBJECT !!!

[not really, but I like being the dissenting voice]

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

end if

End Sub[/code]

[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.

Split got a lot slower on 64 bit, and even with recent optimizations it’s still not up to the old speed.

See https://forum.xojo.com/29075-split-function-in-64bit-is-terribly-slow/0

we also fixed a few edge cases in strings that were not handled correctly
see case 43876

I’m wondering are get/set methods slower than just using a public property?

very slightly