# Optimizing code...

**URL:** <https://forum.xojo.com/t/optimizing-code/39212>\
**Category:** General\
**Created:** [August 14, 2017, 5:40pm UTC](https://forum.xojo.com/t/optimizing-code/39212 "2017-08-14T17:40:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [August 14, 2017, 5:40pm UTC](https://forum.xojo.com/t/optimizing-code/39212/1 "2017-08-14T17:40:12Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [August 14, 2017, 5:41pm UTC](https://forum.xojo.com/t/optimizing-code/39212/2 "2017-08-14T17:41:42Z")

</div>

perhaps the NTHFIELD function

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [August 14, 2017, 5:42pm UTC](https://forum.xojo.com/t/optimizing-code/39212/3 "2017-08-14T17:42:25Z")

</div>

If the parsing ALWAYS looks for a space:

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

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 14, 2017, 6:01pm UTC](https://forum.xojo.com/t/optimizing-code/39212/4 "2017-08-14T18:01:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 14, 2017, 6:08pm UTC](https://forum.xojo.com/t/optimizing-code/39212/5 "2017-08-14T18:08:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [August 14, 2017, 6:24pm UTC](https://forum.xojo.com/t/optimizing-code/39212/6 "2017-08-14T18:24:51Z")

</div>

This regular expression will do the same thing and _might_ be faster. Try something like this:

```auto
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
```

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [August 14, 2017, 6:26pm UTC](https://forum.xojo.com/t/optimizing-code/39212/7 "2017-08-14T18:26:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [August 14, 2017, 8:46pm UTC](https://forum.xojo.com/t/optimizing-code/39212/8 "2017-08-14T20:46:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [August 14, 2017, 9:06pm UTC](https://forum.xojo.com/t/optimizing-code/39212/9 "2017-08-14T21:06:54Z")

</div>

Sorry, what do those numbers mean?

---

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [August 14, 2017, 9:20pm UTC](https://forum.xojo.com/t/optimizing-code/39212/10 "2017-08-14T21:20:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 14, 2017, 9:47pm UTC](https://forum.xojo.com/t/optimizing-code/39212/11 "2017-08-14T21:47:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [August 14, 2017, 10:08pm UTC](https://forum.xojo.com/t/optimizing-code/39212/12 "2017-08-14T22:08:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 14, 2017, 10:15pm UTC](https://forum.xojo.com/t/optimizing-code/39212/13 "2017-08-14T22:15:59Z")

</div>

uh why would anyone object ?

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 15, 2017, 6:19am UTC](https://forum.xojo.com/t/optimizing-code/39212/14 "2017-08-15T06:19:48Z")

</div>

I OBJECT !!!

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

---

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [August 15, 2017, 4:28pm UTC](https://forum.xojo.com/t/optimizing-code/39212/15 "2017-08-15T16:28:02Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [August 15, 2017, 7:29pm UTC](https://forum.xojo.com/t/optimizing-code/39212/16 "2017-08-15T19:29:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 15, 2017, 7:52pm UTC](https://forum.xojo.com/t/optimizing-code/39212/17 "2017-08-15T19:52:17Z")

</div>

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](https://forum.xojo.com/29075-split-function-in-64bit-is-terribly-slow/0)

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 15, 2017, 8:29pm UTC](https://forum.xojo.com/t/optimizing-code/39212/18 "2017-08-15T20:29:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [August 15, 2017, 9:47pm UTC](https://forum.xojo.com/t/optimizing-code/39212/19 "2017-08-15T21:47:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 15, 2017, 10:15pm UTC](https://forum.xojo.com/t/optimizing-code/39212/20 "2017-08-15T22:15:44Z")

</div>

very slightly

[Next page](https://forum.xojo.com/t/optimizing-code/39212.md?page=2)
