Raspberry BASIC

Author Topic: SALC Ubuntu 64  (Read 17147 times)

AIR

  • BASIC Developer
  • *
  • Posts: 16
  • Code Jockey
    • View Profile
SALC Ubuntu 64
« on: December 15, 2019, 08:16:57 AM »
I reworked the FreeBasic submission, and ran it on my Ubuntu PI4:

Code: FreeBasic
  1. ' FreeBasic - 1mil3.bas
  2.  
  3. DIM SHARED a(1000001) AS INTEGER
  4.  
  5. function Reverse( byref source as string ) as string
  6.     dim as integer first = 0, last = Len(source)-1
  7.  
  8.     while( first < last )
  9.         Swap source[first], source[last]
  10.         first += 1 : last -= 1
  11.     wend
  12.  
  13.     return source
  14. end function
  15.  
  16. sub mil()
  17.     dim as string s,t,r
  18.     dim as integer x,c
  19.  
  20.     FOR x = 1 TO 1000000
  21.         c = (x - 1) MOD 26
  22.         s &= CHR(c + 65)
  23.         a(x) = x
  24.         IF c = 25 THEN
  25.             t &= s
  26.             s = ""
  27.             c = 0
  28.         ENDIF
  29.     NEXT
  30.  
  31.     r = Reverse(t)
  32.  
  33.     PRINT "r LEN: " & LEN(r)
  34.     PRINT "Front: " & LEFT(r, 26)
  35.     PRINT "Back:  " & RIGHT(r, 26)
  36.     PRINT "UBVal: " & a(1000000)
  37. end sub
  38.  
  39.  
  40. mil()
  41.  

riveraa@dpi:~/tmp$ timex ./1mil3
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.16user 0.02system 0:00.18elapsed 98%CPU (0avgtext+0avgdata 11596maxresident)k
0inputs+0outputs (0major+2623minor)pagefaults 0swaps


AIR.

EDIT:  Cleaned up code.
« Last Edit: December 15, 2019, 08:37:05 AM by AIR »

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #1 on: December 15, 2019, 08:19:34 AM »
I need to build ScriptBasic on this Ubuntu 64 RPi 4B.

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #2 on: December 15, 2019, 08:47:47 AM »
I think a round 4 is in order but this time running on Ubuntu 64 on the RPi 4B 4GB.
« Last Edit: December 15, 2019, 09:16:03 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #3 on: December 15, 2019, 10:15:24 AM »
ScriptBasic (Ubuntu 64 RPi 4B 4GB)

Code: Script BASIC
  1. ' ScriptBasic - 1mil4.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.  


ubuntu@rpi4b:~/sbrt/examples$ timex scriba 1mil4.sb
t LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
6.60user 0.79system 0:07.42elapsed 99%CPU (0avgtext+0avgdata 277268maxresident)k
0inputs+1960outputs (0major+69241minor)pagefaults 0swaps
ubuntu@rpi4b:~/sbrt/examples$


No big improvement with ScriptBasic under 64 bit on the RPi 4B. Everything built  fine using the Linux dependency list.
« Last Edit: December 15, 2019, 10:18:48 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #4 on: December 16, 2019, 12:07:51 AM »
C (Ubuntu 64 RPi 4B 4GB)

Code: C
  1. // C - 1mil4.c - AIR
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <glib.h>
  6. #include <glib/gprintf.h>
  7.  
  8. int main(int argc, char **argv) {
  9.         GString *s = g_string_new(NULL);
  10.         GString *t = g_string_new(NULL);
  11.         int a[1000001] = {0};
  12.  
  13.         for (int x = 0; x < 1000001; x++) {
  14.                 a[x] = x;
  15.                 g_string_append_c(s,(char)(x%26)+65);
  16.                 if (s->len == 26) {
  17.                         g_string_append(t,g_strreverse(s->str));
  18.                         g_string_assign(s,"");
  19.                 }
  20.         }
  21.  
  22.         g_printf("r LEN: %d\n",t->len);
  23.         g_printf("Front: %.*s\n", 26, t->str);
  24.         g_printf("Back:  %s\n", t->str + t->len - 26);
  25.         g_printf("UBVal: %d\n",a[1000000]);
  26.  
  27.         g_string_free (s,TRUE);
  28.         g_string_free (t,TRUE);
  29. }
  30.  


ubuntu@rpi4b:~/salc/c$ gcc -O3 1mil4.c  $(pkg-config --libs --cflags glib-2.0) -o 1mil4
1mil4.c: In function ‘main’:
1mil4.c:22:27: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘gsize’ {aka ‘long unsigned int’} [-Wformat=]
   22 |         g_printf("r LEN: %d\n",t->len);
      |                          ~^    ~~~~~~
      |                           |     |
      |                           int   gsize {aka long unsigned int}
      |                          %ld
ubuntu@rpi4b:~/salc/c$ ls -l 1mil4
-rwxr-xr-x 1 ubuntu ubuntu 13704 Dec 15 16:03 1mil4
ubuntu@rpi4b:~/salc/c$ timex ./1mil4
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.01user 0.01system 0:00.03elapsed 93%CPU (0avgtext+0avgdata 6576maxresident)k
0inputs+0outputs (0major+1352minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/c$

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #5 on: December 16, 2019, 12:22:58 AM »
Python3 (Ubuntu 64 RPi 4B 4GB)

Code: Python
  1. # Python 3 - 1mil4.py - AIR
  2.  
  3. def main():
  4.     t = []
  5.     r =""
  6.     a = [None] * 1000001
  7.     b = bytearray(26)
  8.     decode = bytearray.decode
  9.     blah = range(1000001)
  10.     append = list.append
  11.  
  12.     for x in blah:
  13.         alpha = x % 26
  14.         b[alpha] = alpha + 65
  15.         a[x] = x
  16.         if alpha == 25:
  17.             append(t, decode(b[::-1]))
  18.  
  19.     r = ''.join(t)
  20.  
  21.     print("r LEN: {}".format(len(r)))
  22.     print("Front: {}".format(r[:26]))
  23.     print("Back:  {}".format(r[-26:]))
  24.     print("UBVal: {}".format(a[1000000]))
  25.  
  26. if __name__ == "__main__":
  27.     main()
  28.  


ubuntu@rpi4b:~/salc/python-dev$ timex python3 1mil4.py
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.49user 0.06system 0:00.56elapsed 99%CPU (0avgtext+0avgdata 52408maxresident)k
0inputs+0outputs (0major+12086minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/python-dev$

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #6 on: December 16, 2019, 01:07:08 AM »
Ruby (Ubuntu 64 RPi 4B 4GB)

Code: Ruby
  1. # Ruby - 1mil4.rb - AIR
  2.  
  3. a = [10000001]
  4. s = ""
  5. t = ""
  6.  
  7.  
  8. (0..1000001).each do |x|
  9.         a << x+1
  10.         s << (x%26)+65
  11.         if s.length == 26
  12.                 t << s.reverse
  13.                 s.clear
  14.         end
  15. end
  16.  
  17. puts "t LEN: #{t.length}"
  18. puts "Front: #{t[0,26]}"
  19. puts "Back:  #{t[-26,26]}"
  20. puts "UBVal: #{a[1000000]}"
  21.  


ubuntu@rpi4b:~/salc/ruby-dev$ timex ruby 1mil4.rb
t LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.68user 0.08system 0:00.88elapsed 86%CPU (0avgtext+0avgdata 18092maxresident)k
2240inputs+0outputs (2major+3678minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/ruby-dev$


« Last Edit: December 16, 2019, 01:17:03 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #7 on: December 16, 2019, 01:26:14 AM »
Perl (Ubuntu 64 RPi 4B 4GB)

Code: Perl
  1. # Perl - 1mil4.pl
  2.  
  3. my $s = "";
  4. my $t = "";
  5. my $a = [1000000];
  6.  
  7. for (my $x = 1; $x <= 1000000; $x++) {
  8.   $s = $s . chr((($x - 1) % 26) + 65);
  9.   $a[$x] = $x;
  10.   if (length($s) == 26) {
  11.     $t = $t . $s;
  12.     $s = "";
  13.   }
  14. }
  15.  
  16.  
  17. my $r = scalar reverse $t;
  18.  
  19. printf("r LEN: %d\n", length($r));
  20. printf("Front: %s\n", substr($r, 0, 26));
  21. printf("Back:  %s\n", substr($r, -26));
  22. printf("UBVal: %d\n", $a[1000000]);
  23.  


ubuntu@rpi4b:~/salc/perl-dev$ timex perl 1mil4.pl
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.85user 0.04system 0:00.90elapsed 99%CPU (0avgtext+0avgdata 38688maxresident)k
0inputs+0outputs (0major+8869minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/perl-dev$

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #8 on: December 16, 2019, 02:53:54 AM »
BaCon (Ubuntu 64 RPi 4B 4GB)

Code: Text
  1. ' BaCon - 1mil4.bac - AIR
  2.  
  3. OPTION PARSE FALSE
  4.  
  5. s$ = ""
  6. t$ = ""
  7.  
  8. DECLARE a[1000001] TYPE NUMBER
  9. DECLARE len TYPE size_t
  10. DECLARE *stream TYPE FILE
  11.  
  12. stream = open_memstream(&t$,&len)
  13.  
  14. FOR x = 1 TO 1000000
  15.   s$ = s$ & CHR$(MOD((x - 1), 26) + 65)
  16.   a[x] = x
  17.   IF LEN(s$) = 26 THEN
  18.     fprintf(stream, "%s", s$)
  19.     s$ = ""
  20.   END IF
  21. NEXT
  22. CLOSE FILE stream
  23.  
  24. r$ = REVERSE$(t$)
  25. FREE t$
  26. PRINT "r LEN: ",len
  27. PRINT "Front: ",LEFT$(r$, 26)
  28. PRINT "Back:  ",RIGHT$(r$, 26)
  29. PRINT "UBVal: ",a[1000000]
  30.  


ubuntu@rpi4b:~/salc/bacon-dev$ bacon 1mil4.bac
WARNING: 7 temporary files found! Do you want to delete them ([y]/n)? y
Temporary files were deleted.
Converting '1mil4.bac'... done, 31 lines were processed in 0.034 seconds.
Compiling '1mil4.bac'... cc  -c 1mil4.bac.c
cc -o 1mil4 1mil4.bac.o -lbacon -L. -lm 
Done, program '1mil4' ready.
ubuntu@rpi4b:~/salc/bacon-dev$ ls -l 1mil4
-rwxr-xr-x 1 ubuntu ubuntu 133856 Dec 15 21:37 1mil4
ubuntu@rpi4b:~/salc/bacon-dev$ timex ./1mil4
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.38user 0.01system 0:00.40elapsed 99%CPU (0avgtext+0avgdata 12096maxresident)k
0inputs+0outputs (0major+3059minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/bacon-dev$

« Last Edit: December 16, 2019, 05:39:45 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #9 on: December 16, 2019, 03:18:57 AM »
Swift (Ubuntu 64 RPi 4B 4GB)

Code: Text
  1. // Swift 5.1.2 ARM 64- 1mil4.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[1000000])
  21.  


ubuntu@rpi4b:~/salc/swift-dev$ swiftc -O 1mil4.swift
ubuntu@rpi4b:~/salc/swift-dev$ ls -l main
-rwxr-xr-x 1 ubuntu ubuntu 74600 Dec 15 19:13 main
ubuntu@rpi4b:~/salc/swift-dev$ timex ./main
t LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal:  1000000
0.65user 0.02system 0:00.69elapsed 98%CPU (0avgtext+0avgdata 16392maxresident)k
0inputs+0outputs (0major+2811minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/swift-dev$


Major improvement for Swift on ARM 64. My guess is that the reason Swift runs like a pig on Rasbian is that Swift depends on the OS compilers rather than using the ones included with the language build like other platforms.
« Last Edit: December 16, 2019, 06:26:04 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #10 on: December 16, 2019, 05:04:24 AM »
PHP (Ubuntu 64 RPi 4B 4GB)

Code: PHP
  1. <?php
  2.  
  3. # PHP -1mil4.php
  4.  
  5. $s = "";
  6. $t = "";
  7. $a = array();
  8.  
  9. for ($x = 1; $x <= 1000000; $x++) {
  10.   $s .= chr((($x - 1) % 26) + 65);
  11.   $a[$x] = $x;
  12.   if(strlen($s) == 26) {
  13.     $t .= $s;
  14.     $s = "";
  15.   }
  16. }
  17.  
  18. $r = strrev($t);
  19.  
  20. echo "r LEN: ", strlen($r), "\n";
  21. echo "Front: ", substr($r, 0, 26), "\n";
  22. echo "Back:  ", substr($r, -26), "\n";
  23. echo "UBVal: ", $a[1000000],"\n";
  24.  
  25. ?>
  26.  


ubuntu@rpi4b:~/salc/php-dev$ timex php 1mil4.php
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000

0.31user 0.10system 0:00.41elapsed 99%CPU (0avgtext+0avgdata 48284maxresident)k
0inputs+0outputs (0major+16200minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/php-dev$


I think many overlook PHP as a fast console scripting language.
ScriptBasic Project Manager/Facilitator

AIR

  • BASIC Developer
  • *
  • Posts: 16
  • Code Jockey
    • View Profile
Re: SALC Ubuntu 64
« Reply #11 on: December 16, 2019, 05:29:05 AM »
BaCon (Ubuntu 64 RPi 4B 4GB)


Compiler error:

Description:
    file '1mil4.bac' line 18: fprintf(stream,s$)
Cause:
    passing argument 2 of 'open_memstream' from incompatible pointer type [-Wincompatible-pointer-types]

ubuntu@rpi4b:~/salc/bacon-dev$


Make the following TWO changes (Ubuntu's gcc is more strict than Rasbian's):

Code: C
  1. DECLARE len TYPE size_t

Code: C
  1. fprintf(stream, "%s", s$)


AIR.

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #12 on: December 16, 2019, 05:40:21 AM »
Thanks!

BaCon post updated.
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: SALC Ubuntu 64
« Reply #13 on: December 17, 2019, 03:16:42 AM »
Java (Ubuntu 64 RPi 4B 4GB)

Code: Java
  1. // Java - 1mil4.java
  2.  
  3. class onemil4
  4.   {
  5.   public static void main(String args[])
  6.   {
  7.   StringBuilder s = new StringBuilder();
  8.   StringBuilder t = new StringBuilder();
  9.   int c = 0;
  10.   int[] a = new int[1000001];
  11.  
  12.   for (int x = 1; x <= 1000000; x++) {
  13.     c = ((x - 1) % 26) + 65;          
  14.     s.append((char) c);
  15.     a[x] = x;
  16.     if(s.length() == 26) {
  17.       t.append(s);
  18.       s.delete(0, s.length());
  19.     }
  20.   }
  21.  
  22.   StringBuilder r = new StringBuilder();
  23.   r.append(t);
  24.   r = r.reverse();
  25.  
  26.   System.out.println("r LEN: " + r.length());
  27.   System.out.println("Front: " + r.substring(0, 26));
  28.   System.out.println("Back:  " + r.substring(r.length() - 26));  
  29.   System.out.println("UBVal: " + a[1000000]);
  30.   }
  31. }
  32.  


ubuntu@rpi4b:~/salc/java-dev$ javac 1mil4.java
ubuntu@rpi4b:~/salc/java-dev$ timex java onemil4
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.83user 0.11system 0:00.68elapsed 137%CPU (0avgtext+0avgdata 38428maxresident)k
0inputs+64outputs (0major+6134minor)pagefaults 0swaps
ubuntu@rpi4b:~/salc/java-dev$

ScriptBasic Project Manager/Facilitator

AIR

  • BASIC Developer
  • *
  • Posts: 16
  • Code Jockey
    • View Profile
Re: SALC Ubuntu 64
« Reply #14 on: December 17, 2019, 03:31:01 AM »
I think you should split the 64bit posts into a separate thread...so they and the 32bit posts are easier to find in the future.