Author |
Topic: Long strings (Read 528 times) |
|
HugoS
New Member
member is offline
Posts: 5
|
|
Long strings
« Thread started on: Mar 26th, 2016, 6:31pm » |
|
Hope this hasn't been addressed before - I did search the forum but zero results were shown.
I have found LBB to be many, many times faster than LB in all respects except when looping through long strings with the word$() function. To put this in perspective, the below code executes in about 15 seconds in LB 4.5, whereas in LBB I gave up somewhere around the 15 minute mark with no end in sight. Appreciate that the easy solution is to split a long string into smaller chunks but I have a good reason for not doing this.
Anyone else experienced the same issue? Is there a workaround? Is it a bug?
The below is just an example so I don't have to upload a data file.
let test$ = httpget$("http://www.dailymail.co.uk/home/index.html") let aPos = instr(test$, "<body") let bPos = instr(test$, "</body>") let aFile$ = mid$(test$, aPos, bPos - aPos)
let a$ = "" while a$ <> "" index = index + 1 a$ = lower$(word$(aFile$, index)) if a$ = "james" then let aCnt = aCnt + 1 end if wend notice aCnt
function httpget$(url$) open "WININET.DLL" for dll as #net calldll #net, "InternetOpenA", "LB Booster" as ptr, 0 as long, _ 0 as long, 0 as long, 0 as long, hnet as ulong calldll #net, "InternetOpenUrlA", hnet as ulong, url$ as ptr, _ "" as ptr, 0 as long, 0 as long, 0 as long, hurl as ulong let buffer$ = space$(1000) struct httpget, nread as long do let buflen = len(buffer$) calldll #net, "InternetReadFile", hurl as ulong, _ buffer$ as ptr, buflen as long, httpget as struct, res as long httpget$ = httpget$ + left$(buffer$, httpget.nread.struct) loop until httpget.nread.struct = 0 calldll #net, "InternetCloseHandle", hurl as ulong, res as long calldll #net, "InternetCloseHandle", hnet as ulong, res as long close #net end function
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Long strings
« Reply #1 on: Mar 26th, 2016, 7:57pm » |
|
on Mar 26th, 2016, 6:31pm, HugoS wrote:I have found LBB to be many, many times faster than LB in all respects except when looping through long strings with the word$() function. |
|
Sorry, word$() is slow. The explanation is that there is no direct equivalent in BBC BASIC, so it has to be emulated at run-time - in interpreted BBC BASIC code!
If you've read the How It Works section of the LBB Help file you'll know that LBB is a hybrid translator and emulator. Statements and functions that have a direct equivalent in BBC BASIC are translated at 'compile time' and typically run up to ten times more quickly (possibly even faster). However features that don't have a direct equivalent in BBC BASIC are emulated at run time and typically run at about the same speed as, and sometimes slower than, LB 4.
Could you not do it this way, which is a lot faster than using word$(), and indeed a lot faster than your code running in LB 4.04:
Code: test$ = httpget$("http://www.dailymail.co.uk/home/index.html")
aPos = instr(test$, "<body")
bPos = instr(test$, "</body>")
aFile$ = mid$(test$, aPos, bPos - aPos)
aFile$ = lower$(aFile$)
toFind$ = "james"
aCnt = 0
i = 0
do
i = instr(aFile$, toFind$, i+1)
if i then
if trim$(mid$(aFile$, i-1, len(toFind$)+2)) = toFind$ then
aCnt += 1
end if
end if
loop until i = 0
notice aCnt
end
'include lb45func.bas
The actual counting process (i.e. excluding loading the file from the web) took 0.04 seconds here!!
Richard.
|
|
|
|
HugoS
New Member
member is offline
Posts: 5
|
|
Re: Long strings
« Reply #2 on: Mar 26th, 2016, 9:09pm » |
|
on Mar 26th, 2016, 7:57pm, Richard Russell wrote:The actual counting process (i.e. excluding loading the file from the web) took 0.04 seconds here!! |
|
Literally a blink of an eye! Much better, thank you Richard.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Long strings
« Reply #3 on: Mar 27th, 2016, 12:19am » |
|
on Mar 26th, 2016, 9:09pm, HugoS wrote:Literally a blink of an eye! Much better, thank you Richard. |
|
I should perhaps add that if there is no reasonable alternative to word$(), specifying the delimiter - if you are able to - will make it considerably quicker than the default case (when it is having to treat any 'whitespace' as the delimiter).
Richard.
|
|
Logged
|
|
|
|
|