Better to use string modification by pointer or via return?

Just starting out? Need help? Post your questions and find answers here.
highend
Enthusiast
Enthusiast
Posts: 123
Joined: Tue Jun 17, 2014 4:49 pm

Better to use string modification by pointer or via return?

Post by highend »

Hi,

This is a question of "usability" (how do _you_ do it)...

Following example code:

Code: Select all

; Test if a reference for a string (file or path) is already expanded
; Local path           : C:\Windows
; Network path         : \\server\share
; Expanded local path  : \\?\C:\Windows
; Expanded network path: \\?\UNC\server\share

; How to test:
; If the 3rd character <> "?" => NOT expanded!
Procedure.i isExpanded(*item)
  Protected.Character *c = *item

  ; Reference empty
  If *c = 0 Or *c\c = 0
    ProcedureReturn #False
  EndIf

  ; Advance to 3rd character
  *c + (2 * SizeOf(Character))
  If *c\c <> '?'
    ProcedureReturn #False
  EndIf

  ProcedureReturn #True
EndProcedure


Procedure.s ExpandPath1(*path.String)
  ; Only process if not expanded
  If Not isExpanded(@*path\s)
    ; Prefix it
    ; Advance to 2nd character
    Protected.Character *c = @*path\s + SizeOf(Character)

    ; Local path
    If *c\c = ':'
      ProcedureReturn "\\?\" + *path\s

    ; Network path
    Else
      ProcedureReturn "\\?\UNC" + Mid(*path\s, 2)
    EndIf
  EndIf
EndProcedure
;CompilerEndIf


Procedure.i ExpandPath2(*path.String)
  ; Only process if not expanded
  If Not isExpanded(@*path\s)
    ; Prefix it
    ; Advance to 2nd character
    Protected.Character *c = @*path\s + SizeOf(Character)

    ; Local path
    If *c\c = ':'
      *path\s = "\\?\" + *path\s

    ; Network path
    Else
      *path\s = "\\?\UNC" + Mid(*path\s, 2)
    EndIf
  EndIf
EndProcedure


; Test
Define path.String
path\s = "C:\Windows"
For i = 1 To 1000000 ; 1 million
  ;ExpandPath1(@path)
  ;ExpandPath2(@path)
Next
Testing with "ExpandPath1(@path)" enabled: ~220-300 ms
Testing with "ExpandPath2(@path)" enabled: ~20-30 ms

So obviously ExpandPath2() is ~10 times faster and leads to the same result but with
"ExpandPath1()" you can do this:

Code: Select all

Define path.String
path\s = "C:\Windows"
If SomeFunction(ExpandPath1(@path))
  ...
EndIf
while with
"ExpandPath2()" you need to do this:

Code: Select all

Define path.String
path\s = "C:\Windows"
ExpandPath2(@path)
If SomeFunction(path\s)
  ...
EndIf
What do you prefer, choose the second method because of its superior speed or stay with the first one because of it's simplicity?