For the moment I've developed a new function called MIDF$ (fast version of MID$)
Instead of accepting as first parameter a string expression it accept a scalar string variable.
In this way it is able to determine string pointer and get needed bytes without the need to create a local temporary copy of the string expression.
Results are this one:
length of the numeric text = 270000
APPROACH_STRING1OVERLAY 0.243s, result = 1350000
APPROACH_MIDF 0.220s, result = 1350000 <--------FAST version of MID$
APPROACH_STAT 0.003s, result = 1350000
APPROACH_MID 7.070s, result = 1350000
APPROACH_STRING1OVERLAYWITHARRAYSUM 0.038s, result = 1350000
MIDF$ unction will be present in next thinBasic update.
Full code:
Uses "console"uses "stat"
function TBMain()
' -- Setup
hiResTimer_Init
string input = repeat$(30000, "123456789")
printL "length of the numeric text = " + len(input)
printl
' -- List all user functions to array
string functions()
function_list(functions, %Function_User)
' -- Benchmark all functions matching signature "APPROACH_*"
for f as long = 1 to countOf(functions)
if isLike(functions(f), "APPROACH_*", false) then
Benchmark(functions(f), input)
end if
next
waitKey
end function
function Benchmark(functionName as string, parameter as string) as long
ext delta
long result
hiResTimer_Get
' -- Call function by name
call functionName(parameter) to result
delta = hiResTimer_Delta ' Delta since last hiResTimer_* call
printl lset$(functionName, 50 using " "), ' -- Pad to left with spaces
format$(delta/1000000, "0.000") + "s" + ' -- Format it like we are interested in zero and 3 decimals
", result = " + result
return result
end function
function Approach_Mid(byRef input as string) as long
long result
for index as long = 1 to len(input)
result + mid$(input, index, 1)
next
return result
end function
function Approach_MidF(byRef input as string) as long
long result
for index as long = 1 to len(input)
result + midf$(input, index, 1)
next
return result
end function
function Approach_String1Overlay(byRef input as string) as long
dim TXT(len(input)) as string * 1 at strPtr(input)
long result
for index as long = 1 to len(input)
result + TXT(index)
next
return result
end function
function Approach_String1OverlayWITHArraySum(byRef input as string) as long
dim TXT(len(input)) as string * 1 at strPtr(input)
return Array sum TXT
end function
function Approach_Stat(byRef input as string) as long
dim ascii(len(input)) as byte at strPtr(input)
return stat_sum(ascii) - len(input)*48 ' -- Ha ha! ASCII values of numbers start at 48
end function
Bookmarks