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

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #120 on: December 09, 2019, 10:26:43 AM »
Thanks!

I forgot to do Ruby for round three. There is a round two Ruby post in the challenge.

Offline jalih

  • Advocate
  • Posts: 89
Re: RaspberryBASIC.org Forum
« Reply #121 on: December 09, 2019, 11:11:24 AM »
Anyway, I'm looking forward to seeing how you choose to implement this, Jalih.

I will post my fully featured version for 8th using simple string builder later when I get back home. Here is naive version using dynamic buffers. Size of buffers is initially 0.
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

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #122 on: December 09, 2019, 11:20:50 AM »
AIR,

I seem to have a problem compiling your latest C  code.


pi@RPi4B:~/c-dev/examples $ gcc -O3 1mil3-3.c  $(pkg-config --libs --cflags glib-2.0) -o 1mil3-3
1mil3-3.c: In function ‘main’:
1mil3-3.c:28:9: error: expected declaration or statement at end of input
         g_string_free (t,TRUE);
         ^~~~~~~~~~~~~
pi@RPi4B:~/c-dev/examples $


Offline AIR

  • BASIC Developer
  • Posts: 782
Re: RaspberryBASIC.org Forum
« Reply #123 on: December 09, 2019, 01:11:43 PM »
Usually caused by a missing bracket or semi-colon.  You sure you copied all of the code including end bracket?

Edit:  I copy/pasted from the post onto another machine (a mac), compiled/executed fine here.
« Last Edit: December 09, 2019, 01:27:44 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #124 on: December 09, 2019, 03:10:00 PM »
Yep. Missed the final }.

Suggestion: Put your code tags on separate lines. I have got caught before not copying your code tags which had code attached.
« Last Edit: December 09, 2019, 03:20:51 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 782
Re: RaspberryBASIC.org Forum
« Reply #125 on: December 09, 2019, 04:59:41 PM »
Rust.  Not liking this one too much personally from a syntax standpoint, but you had asked for a Rust submission in the beginning...

Code: Text
  1. fn main() {
  2.     let mut s = "".to_string();
  3.     let mut t = "".to_string();
  4.     let mut a = [0;1000001];
  5.  
  6.     for x in 0..1000001 {
  7.         a[x] = x;
  8.         let b   = ( x%26 ) as u8;
  9.         let c = ( b+65 ) as char;
  10.         s.push( c );
  11.         if s.len() == 26 {
  12.             let reversed: String = s.chars().rev().collect();
  13.             t.push_str( &reversed );
  14.             s.clear();
  15.         }
  16.     }
  17.     println!( "r LEN: {}",  t.len() );
  18.     println!( "Front: {}",  &t[..26] );
  19.     println!( "Back:  {}",  &t[t.len()-26..]);
  20.     println!( "UBVal: {}",  a[1000000] );
  21.  
  22. }
  23.  


riveraa@rpi:~/Projects/rust/1mil3/mil $ timex target/release/mil
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.07user 0.02system 0:00.10elapsed 99%CPU (0avgtext+0avgdata 6356maxresident)k
0inputs+0outputs (0major+1392minor)pagefaults 0swaps


AIR.

EDIT: arm binary attached.
« Last Edit: December 09, 2019, 05:48:32 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #126 on: December 09, 2019, 05:44:15 PM »
Thanks Air for the rust submission. Can you attach the binary so I don't have to install Rust?

Damn. That is one big executable. If Microsoft rewrites Windows in Rust, you will need a terabyte of space just to load the OS.

rwxr-xr-x 1 pi pi 2582500 Dec  9 17:47 1mil3

pi@RPi4B:~/rust-dev/examples $ timex ./1mil3
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.10user 0.02system 0:00.12elapsed 96%CPU (0avgtext+0avgdata 6336maxresident)k
0inputs+0outputs (0major+1392minor)pagefaults 0swaps
pi@RPi4B:~/rust-dev/examples $
« Last Edit: December 09, 2019, 08:19:33 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #127 on: December 09, 2019, 06:51:44 PM »
The ARM Swift maintainer posted an alternative to my script which I'll try and post its results.

No improvement with his substitute string reverse function.

Code: Text
  1. // Swift 5.1.2 - 1mil3-2.swift
  2.  
  3. var s = ""
  4. var t = ""
  5. var a = [Int]()
  6.  
  7. for x in 1...1000000 {
  8.   s += String(UnicodeScalar(UInt8(((x - 1) % 26) + 65)))
  9.   a.append(x)
  10.   if s.count == 26 {
  11.     t += s
  12.     s = ""
  13.   }
  14. }
  15.  
  16. var r = Array(t)
  17. let count = t.count
  18. for i in 0..<count/2 {
  19.   r.swapAt(i, count - ((i + 1) as Int))
  20. }
  21.  
  22. let revstr=String(r)
  23.  
  24. print("r LEN: ", revstr.count)
  25. print("Front: \(revstr.prefix(26))")
  26. print("Back:  \(revstr.suffix(26))")
  27. print("UBVal: ", a[100000 - 1])
  28.  


pi@RPi4B:~/swift-dev/examples $ timex ./main
r LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal:  100000
317.29user 0.09system 5:17.71elapsed 99%CPU (0avgtext+0avgdata 25444maxresident)k
7096inputs+0outputs (32major+6377minor)pagefaults 0swaps
pi@RPi4B:~/swift-dev/examples $



« Last Edit: December 09, 2019, 07:41:15 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 782
Re: RaspberryBASIC.org Forum
« Reply #128 on: December 09, 2019, 08:29:43 PM »
Old School (Pascal)
FreePascal:

Code: Pascal
  1. program mil;
  2.  
  3. Uses StrUtils;
  4.  
  5. var
  6.     s:AnsiString;
  7.     t:AnsiString;
  8.     c:AnsiChar;
  9.     a:array[0..1000001] of int32;
  10.     x:int32;
  11. begin
  12.     s := '';
  13.     t := '';
  14.     for x := 0 to 1000001 do
  15.     begin
  16.         c :=  chr( (x mod 26)+65 );
  17.         a[x] := x;
  18.         s += c;
  19.         if Length(s) = 26 then
  20.         begin
  21.             t += ReverseString(s);
  22.             s := '';
  23.         end;
  24.     end;
  25.  
  26.     writeln( 'r len: ', Length(t) );
  27.     writeln( 'Front: ', LeftStr(t,26) );
  28.     writeln( 'Back:  ', RightStr(t,26) );
  29.     writeln( 'UBVal: ', a[1000000]);
  30. end.
  31.  
  32.  

riveraa@rpi:~/Projects/fpc/1mil3 $ fpc -O3 -XX -Xs  mil.pas

riveraa@rpi:~/Projects/fpc/1mil3 $ timex ./mil
r len: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.19user 0.02system 0:00.22elapsed 99%CPU (0avgtext+0avgdata 5604maxresident)k
0inputs+0outputs (0major+1430minor)pagefaults 0swaps


AIR.

EDIT:  Binary attached.
« Last Edit: December 09, 2019, 08:32:33 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #129 on: December 09, 2019, 09:10:59 PM »
Wow!

You're like a language machine.   8)

Offline AIR

  • BASIC Developer
  • Posts: 782
Re: RaspberryBASIC.org Forum
« Reply #130 on: December 09, 2019, 09:20:46 PM »
Pascal was my first "REAL" language (via Delphi 6 on Windows a long time ago).

As far a Swift:  The problem is not the Reverse, the problem is the adding to the string.  Looks like it's creating a new string each time you try to add to it.

Try changing "+=" to ".append". like this:

Code: Text
  1. s.append(String(UnicodeScalar(UInt8(((x - 1) % 26) + 65))))

and

Code: Text
  1. t.append(s)

On my Mac, with the array portion commented out, it took the execution from 0m2.056s down to 0m0.673s.

The next biggest issue is all of the intermediate objects that are created with the crazy casting you have to do to get the integer over to a Character then to a String.  That adds up quickly with the loop we have going.

AIR.


Offline AIR

  • BASIC Developer
  • Posts: 782
Re: RaspberryBASIC.org Forum
« Reply #131 on: December 09, 2019, 10:11:08 PM »
John, try this:

Code: Text
  1. / Swift 5.1.2 - 1mil3.swift
  2.  
  3. var s = ""
  4. var t = ""
  5. var a = [Int](repeating: 0, count: 1000001)
  6.  
  7. for x in 1...1000000 {
  8.   let c = (x - 1) % 26
  9.   s.append(String(UnicodeScalar(UInt8(c + 65))))
  10.   a[x] = x
  11.   if c == 25 {
  12.     t.append(String(s.reversed()))
  13.     s = ""
  14.   }
  15. }
  16.  
  17. // let r = t
  18.  
  19. print("r LEN: ", t.count)
  20. print("Front: \(t.prefix(26))")
  21. print("Back:  \(t.suffix(26))")
  22. print("UBVal: ", a[100000])
  23.  
  24.  

On my Mac, not installing Swift on my RPI:


[riveraa@mini ~/Projects/Swift/1mil3] $ swift build -c release
Compile Swift Module '_mil3' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/release/1mil3
[riveraa@mini ~/Projects/Swift/1mil3] $ time .build/release/1mil3
r LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal:  100000

real    0m0.338s
user    0m0.323s
sys    0m0.010s


AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #132 on: December 09, 2019, 10:12:54 PM »
With the append and doing string reverse on  the fly, we went from 5 minutes to 43 seconds.

Code: Text
  1. // Swift 5.1.2 - 1mil3-2.swift
  2.  
  3. var s = ""
  4. var t = ""
  5. var a = [Int]()
  6.  
  7. for x in 1...1000000 {
  8.   s.append(String(UnicodeScalar(UInt8(((x - 1) % 26) + 65))))
  9.   a.append(x)
  10.   if s.count == 26 {
  11.     let r = String(s.reversed())
  12.     t.append(r)
  13.     s = ""
  14.   }
  15. }
  16.  
  17. print("t LEN: ", t.count)
  18. print("Front: \(t.prefix(26))")
  19. print("Back:  \(t.suffix(26))")
  20. print("UBVal: ", a[100000 - 1])
  21.  


pi@RPi4B:~/swift-dev/examples $ swiftc  1mil3-2.swift
pi@RPi4B:~/swift-dev/examples $ timex ./main
t LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal:  100000
43.64user 0.03system 0:43.69elapsed 99%CPU (0avgtext+0avgdata 12984maxresident)k
0inputs+0outputs (0major+2929minor)pagefaults 0swaps
pi@RPi4B:~/swift-dev/examples $


Offline AIR

  • BASIC Developer
  • Posts: 782
Re: RaspberryBASIC.org Forum
« Reply #133 on: December 09, 2019, 10:15:37 PM »
Try what I posted, making sure you build in "release" mode.  Default is "debug", which is slower.

[riveraa@mini ~/Projects/Swift/1mil3] $ /usr/bin/time .build/release/1mil3
r LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal:  100000
        0.32 real         0.31 user         0.00 sys


/usr/bin/time is different on Mac.

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2969
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #134 on: December 09, 2019, 10:19:50 PM »
I use swiftc to compile.

Your latest cut it in  half again.

Code: Text
  1. // Swift 5.1.2 - 1mil3-3.swift
  2.  
  3. var s = ""
  4. var t = ""
  5. var a = [Int](repeating: 0, count: 1000001)
  6.  
  7. for x in 1...1000000 {
  8.   let c = (x - 1) % 26
  9.   s.append(String(UnicodeScalar(UInt8(c + 65))))
  10.   a[x] = x
  11.   if c == 25 {
  12.     t.append(String(s.reversed()))
  13.     s = ""
  14.   }
  15. }
  16.  
  17. print("t LEN: ", t.count)
  18. print("Front: \(t.prefix(26))")
  19. print("Back:  \(t.suffix(26))")
  20. print("UBVal: ", a[100000])
  21.  


pi@RPi4B:~/swift-dev/examples $ swiftc  1mil3-3.swift
pi@RPi4B:~/swift-dev/examples $ timex ./main
t LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal:  100000
24.09user 0.04system 0:24.19elapsed 99%CPU (0avgtext+0avgdata 12656maxresident)k
0inputs+0outputs (0major+1907minor)pagefaults 0swaps
pi@RPi4B:~/swift-dev/examples $

« Last Edit: December 09, 2019, 10:59:48 PM by John »