Author Topic: RaspberryBASIC.org Forum  (Read 99535 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #75 on: December 07, 2019, 07:08:20 PM »
So here's something interesting.

I modified the Python entry as follows:

Code: Python
  1. #!/usr/bin/python3
  2.  
  3. import string
  4.  
  5. s = ""
  6. t = ""
  7. i = 0
  8. a = [None] * 1000001
  9.  
  10. for x in range(1000001):
  11.   i += 1
  12.   a[x] = x
  13.   if i == 26:
  14.     t += string.ascii_uppercase
  15.     i = 0
  16.  
  17. r = ''.join(reversed(t))
  18.  
  19. print("r LEN: {}".format(len(r)))
  20. print("Front: {}".format(r[:26]))
  21. print("Back:  {}".format(r[-26:]))
  22. print("UBVal: {}".format(a[1000000]))
  23.  

riveraa@rpi:~ $ /usr/bin/time ./mil.py
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.74user 0.09system 0:00.83elapsed 99%CPU (0avgtext+0avgdata 32648maxresident)k
0inputs+0outputs (0major+7178minor)pagefaults 0swaps


HOWEVER, the same code executed using Python2 shows:

riveraa@rpi:~ $ /usr/bin/time ./mil.py
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.57user 0.11system 0:00.68elapsed 99%CPU (0avgtext+0avgdata 26680maxresident)k
0inputs+0outputs (0major+6956minor)pagefaults 0swaps


Personally, after years of using Python2 I still haven't made the switch.  This is potentially another reason why I won't.

Food for thought, anyway...

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #76 on: December 07, 2019, 07:37:27 PM »
Nice but it doesn't meet the goals of the challenge.

No MOD function building a character
No sub-string that appends the result string then clears it for reuse.


Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #77 on: December 07, 2019, 07:43:50 PM »
I posted this to show the difference in string performance; Python 3's UNICODE strings introduce additional overhead vs Python 2.
Did you ever post a formal spec for this challenge?  If so I must have missed it.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #78 on: December 07, 2019, 08:16:16 PM »
The ScriptBasic example was the template for the challenge. Jalih wanted to use buffers and I explained the challenge rules in the response to him.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #79 on: December 07, 2019, 08:18:36 PM »
The ScriptBasic example was the template for the challenge. Jalih wanted to use buffers and I explained the challenge rules in the response to him.

A formal spec details what's required and what isn't allowed, John.  Code isn't a formal spec, it's just code.  No worries, just clarifying.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #80 on: December 07, 2019, 09:00:38 PM »
Basically it's you and I contributing to this challenge. If one looks at the code for each language you will quickly see a pattern. I wasn't counting on anyone else jumping in. Thanks for your many contributions to make this have an informative result.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #81 on: December 07, 2019, 09:16:08 PM »
FreeBasic could use a makeover of it's StringReverse code. A million MID()S has to be painful. I was thinking a pointer swap in a loop.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #82 on: December 07, 2019, 09:34:03 PM »
ScriptBasic updated.

Code: ScriptBasic
  1. ' ScriptBasic - 1milfs.sb
  2.  
  3. OPEN  "t_file" FOR OUTPUT AS #1
  4.  
  5. s = ""
  6. SPLITA STRING(1000001,"0") BY "" TO a
  7.  
  8. FOR x = 1 TO 1000000
  9.   s &= CHR(((x - 1) % 26) + 65)
  10.   a[x] = x
  11.   IF LEN(s) = 26 THEN
  12.     PRINT #1, s
  13.     s = ""
  14.   END IF
  15. NEXT
  16. CLOSE(1)
  17.  
  18. flen = FILELEN("t_file")
  19. OPEN "t_file" FOR INPUT AS #1
  20. t = INPUT(flen, 1)
  21. CLOSE(1)
  22. t = STRREVERSE(t)
  23.  
  24. PRINT "t LEN: ",LEN(t),"\n"
  25. PRINT "Front: ",LEFT(t, 26),"\n"
  26. PRINT "Back:  ",RIGHT(t, 26),"\n"
  27. PRINT "UBVal: ",a[1000000],"\n"
  28.  


pi@RPi4B:~/sbrt/examples $ timex scriba 1milsf.sb
t LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
7.03user 0.84system 0:07.89elapsed 99%CPU (0avgtext+0avgdata 171504maxresident)k
0inputs+1976outputs (0major+43080minor)pagefaults 0swaps
pi@RPi4B:~/sbrt/examples $

« Last Edit: December 07, 2019, 11:01:39 PM by John »

Offline jalih

  • Advocate
  • Posts: 111
Re: RaspberryBASIC.org Forum
« Reply #83 on: December 08, 2019, 01:54:34 AM »
I have an updated 8th code that uses buffers instead of strings and should be at least ten times faster than my previous version. I will post it later when I get back home.

Offline jalih

  • Advocate
  • Posts: 111
Re: RaspberryBASIC.org Forum
« Reply #84 on: December 08, 2019, 04:35:07 AM »
Here is updated 8th code:
Code: [Select]
1000000 constant LIMIT

a:new 0 a:push var, a
0 b:new true b:writable var, s
0 b:new true b:writable var, t


: iterate
  s @ "" 2 pick n:1- 26 n:mod 65 n:+ s:+ b:append
  b:len 26 n:< not if
    t @ swap b:append drop
    0 b:new true b:writable s !
  else
    drop
  then
  a:push ;

: app:main
  a @ ' iterate 1 LIMIT loop
  t @ b:rev >s s:len "r LEN: %d\n" s:strfmt .
  dup 26 s:lsub "Front: %s\n" s:strfmt .
  26 s:rsub "Back:  %s\n" s:strfmt .
  LIMIT a:@ nip "UBVal: %d\n" s:strfmt .
  bye ;

RPI binary available here

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #85 on: December 08, 2019, 08:13:55 AM »
@Jalih:  I get an error when executing your binary:

./r3
Exception: Expected Variable but got Buffer: : G:@: task REPL


@John: If this challenge is about evaluating over-all string performance for a given language, wouldn't saving to a file (bypassing appending to a string in memory) run counter to evaluating over-all string performance?  I'm basing this on what you said about the original SB submission being the 'spec' for this challenge.

AIR.

Offline jalih

  • Advocate
  • Posts: 111
Re: RaspberryBASIC.org Forum
« Reply #86 on: December 08, 2019, 08:23:45 AM »
Big speed boost with Go and Strings.Builder comes from not having to allocate memory all the time, but using buffer for slice and only growing it some amount when needed.

There is a handy feature on 8th, that allows associating data item with some extra data. That allows one to easily write custom string builder for ascii strings.
Code: [Select]
1000000 constant LIMIT

a:new 0 a:push var, a
26 b:new true b:writable b:clear 0 extra! var, s
LIMIT b:new true b:writable b:clear 0 extra! var, t

: write-byte  \ b byte -- b
  swap extra@ rot b:!
  extra@ n:1+ extra! ;

: write  \ b1 b2 -- b1
  extra@ dup >r 0 swap b:slice >r
  extra@ r> swap b:splice
  extra@ r> n:+ extra! ;

: reset  \ b -- b
  b:clear
  0 extra! ;

: len  \ b -- b n
  extra@ ;

: slice
  extra@ 0 swap b:slice ;

: rev  \ b - b
  extra@ swap b:rev swap extra! ;

: iterate
  s @ over n:1- 26 n:mod 65 n:+ write-byte
  len 26 n:< not if
    t @ swap write drop
    s @ reset drop
  else
    drop
  then
  a:push ;

: app:main
  a @ ' iterate 1 LIMIT loop
  t @ slice rev >s s:len "r LEN: %d\n" s:strfmt .
  dup 26 s:lsub "Front: %s\n" s:strfmt .
  26 s:rsub "Back:  %s\n" s:strfmt .
  LIMIT a:@ nip "UBVal: %d\n" s:strfmt .
  bye ;

General purpose version should test if there is enough room on the buffer and grow buffer, if needed.
« Last Edit: December 08, 2019, 08:46:56 AM by jalih »

Offline jalih

  • Advocate
  • Posts: 111
Re: RaspberryBASIC.org Forum
« Reply #87 on: December 08, 2019, 08:28:38 AM »
@Jalih:  I get an error when executing your binary:

./r3
Exception: Expected Variable but got Buffer: : G:@: task REPL

Have to take look, I might have accidentally build some work in progress version...

Offline jalih

  • Advocate
  • Posts: 111
Re: RaspberryBASIC.org Forum
« Reply #88 on: December 08, 2019, 08:39:01 AM »
Here should be working 8th binary for my version.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #89 on: December 08, 2019, 09:21:23 AM »
AIR,

I used the same concept you did for BaCon to speed up its dynamic result 'string'.

The only rule is you can't use fixed size buffers. Storage needs to be dynamic.
« Last Edit: December 08, 2019, 09:34:33 AM by John »