Author Topic: Slow string handling  (Read 3657 times)

0 Members and 1 Guest are viewing this topic.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Slow string handling
« on: July 15, 2019, 03:29:30 pm »
I've just had a problem with the following program- here is the relevent subroutine.
Code: QB64: [Select]
  1. addr = 32768
  2.     k = k + 1
  3.     IF k > addr THEN EXIT DO
  4.     FOR i = 1 TO 32 STEP 2
  5.         xi = ASC(MID$(x(k), i, 1)) + ASC(MID$(x(k), i + 1, 1)) * 256
  6.         PRINT xi;
  7.     NEXT i
  8.     PRINT k
  9.  

The problem is it takes just over 10 minutes to run on version 1.2 of QB64
On version 0.954 it takes 23 seconds.
That is it is 26x slower on the newer version!
I would have put this in "bugs" but it does not seem I can post there.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Slow string handling
« Reply #1 on: July 15, 2019, 03:49:06 pm »
Well, we can't test it, because you did not include the string x().

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Slow string handling
« Reply #2 on: July 15, 2019, 03:52:45 pm »
And what, if you dont use MID$ but both parameters in ASC (string, position) -> ASC ("Test", 2) return ASC for "e".

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Slow string handling
« Reply #3 on: July 15, 2019, 04:32:22 pm »
Isn’t this basically MKI$ And CVI at work?  Convert an integer to a 2-byte string, and then back again....

http://www.qb64.org/wiki/MKI$
http://www.qb64.org/wiki/CVI

Seems like you could do the work quickly with:
xi = CVI(MID$(x(k),i,2)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Slow string handling
« Reply #4 on: July 15, 2019, 04:43:38 pm »
And what, if you dont use MID$ but both parameters in ASC (string, position) -> ASC ("Test", 2) return ASC for "e".

And what if you test on vers 1.3, and default all variables to Long?
Code: QB64: [Select]
  1. 'test on QB64 v1.3
  2. DEFLNG A-Z
  3. 'need 32768 sample strings 32+ chars long
  4. CONST addr = 32768
  5. DIM x(0 TO 32768) AS STRING, k, i, xi, b$, start!, w$
  6.  
  7. 'setup
  8. FOR k = 1 TO addr
  9.     b$ = ""
  10.     FOR i = 1 TO 32
  11.         b$ = b$ + CHR$(INT(RND * 255) + 1)
  12.     NEXT
  13.     x(k) = b$
  14.     PRINT k ', b$
  15. k = 0
  16. INPUT "OK enter to time test..."; w$
  17. start! = TIMER
  18.     k = k + 1
  19.     IF k > addr THEN EXIT DO
  20.     FOR i = 1 TO 32 STEP 2
  21.         xi = ASC(x(k), i) + ASC(x(k), i + 1) * 256
  22.         PRINT xi;
  23.     NEXT i
  24.     PRINT k
  25. PRINT "Time"; TIMER - start!
  26.  
  27.  

I did find that printing out all the crazy random ASCII in setting up test for x(i), THAT took forever just to get 1000!

« Last Edit: July 15, 2019, 04:45:05 pm by bplus »

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Slow string handling
« Reply #5 on: July 16, 2019, 08:12:37 am »
Well it seems that it is printing to the screen that is slow in version 1.2
Of course it helps using numbers rather than strings, but this is a small saving compared to suspressing screen output.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(680, 800, 2)
  2. font1& = _LOADFONT("c:\windows\fonts\times.ttf", 12)
  3. _FONT font1&
  4. ON ERROR GOTO handler
  5. k = 1
  6. addr = 0
  7. OPEN "c:\cw1\pressout1.txt" FOR OUTPUT AS #2
  8. OPEN "com6:115200,N,8,1,CS0,DS0" FOR RANDOM AS #1 LEN = 2 '115200 19200 9600
  9.     WHILE LOC(1) < 2: WEND
  10.     addr = addr + 1
  11.     GET #1, , x(addr)
  12.     'x(addr) = a
  13.     PRINT #2, USING "######"; x(addr);
  14.     IF addr MOD 16 = 0 THEN PRINT #2, " "
  15.     IF addr MOD 1000 = 0 THEN PRINT addr
  16.     IF addr > 524287 THEN EXIT DO
  17.  
the code above works (though it is still slightly slower than QB64 v0.954).
The program takes input from a COM port and writes it to a file.
The writing to a file on the hard drive seems quick as it can be achieved at the same time as reading form the port.
The COM port is actually working at about 190,000 baud (524288 2byte integers in about 44 seconds)

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Slow string handling
« Reply #6 on: July 16, 2019, 11:21:04 am »
A further revelation.
It is actually specifying the TTF font for the output that is causing the slowness.
Using the default font and it is quick.

Also I cannot seem to get QB64 v1.3 to run

the compilelog shows the following
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Slow string handling
« Reply #7 on: July 16, 2019, 12:42:52 pm »
Is this a clean installation to a new folder?

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Slow string handling
« Reply #8 on: July 16, 2019, 12:50:46 pm »
Yes it seems to have unzipped correctly into a new folder.
Does the folder need to have a specific name?

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Slow string handling
« Reply #9 on: July 16, 2019, 01:24:57 pm »
"new folder"

That has a space in it.

Well, you could try renaming it newfolder, no space, and give it a try.

I just unzip the qb64 folder straight off the C:\ drive. Anyway, this is just a shot in the dark, but SHELL calls, for instance, require double quotes to recognize path names with space(s) in them.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Slow string handling
« Reply #10 on: July 16, 2019, 01:41:37 pm »
The folder may not have a specific name, but it may be that you may need to set the necessary permissions for QB64.exe if you install it on a system disk. I suppose we're talking about installing under Windows. I have QB64 on a disk where there is no system and have none problem.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Slow string handling
« Reply #11 on: July 16, 2019, 02:38:34 pm »
How strange I've downloaded the file (qb64v1.3) again and it now works.

Anyway thanks for the help you made me try different things until it became obvious the the only thing left was the font declaration.
Maybe there should be a note in the WIKI that using fancy fonts can/will slow down execution.

Just a final note on the serial communication. My computer is acting as a "slave' so the external device is dictating the baud rate.
So with an external "master" QB64 will accept data at a rate greater than is given in the specifications.
(It doesn't matter what rate I set in the OPEN COM statement).
Just for fun I've tried overclocking, but it would seem that the limit is about 250 kbaud not much more than the 190 kbaud measure when running at normal speed.
Even so despite these blistering rates (for a standard serial port) it takes 44seconds to transfer 1 Mbyte.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Slow string handling
« Reply #12 on: July 16, 2019, 03:09:54 pm »
times.ttf is a variable-width font, so it’s going to be much slower to render and draw.  If you load a monospace font, you probably won’t see anywhere near the same delay.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!