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

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #30 on: December 01, 2019, 08:44:47 PM »
Last one I'm doing.

NIM
Code: [Select]
proc reverse*(str: string): string =
  result = ""
  for index in countdown(str.high, 0):
    result.add(str[index])

type
    milarray = array[1000001, int]

var
    s = ""
    t = ""
    r:string
    a:milarray

for x in 0..1000000:
    a[x] = x

    s &= chr((x mod 26) + 65)

    if s.len == 26:
        t &= s
        s = ""


r = t.reverse
echo "r Len: ",r.len
echo "Front: ",r[0..25]
echo "Back:  ",r[r.len-26..r.len-1]
echo "UBVal: ",a[1000000]

On my RPI 3B+:

riveraa@dpi:~ $ time ./mil
r Len: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000

real    0m0.123s
user    0m0.104s
sys    0m0.019s


AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #31 on: December 01, 2019, 09:02:49 PM »
Wow!

Nim is fast.


pi@RPi4B:~/nim-dev/examples $ nim c -d:release onemil3.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: system [Processing]
Hint: onemil3 [Processing]
Hint:  [Link]
Hint: operation successful (12410 lines compiled; 0.566 sec total; 10.754MiB peakmem; Release Build) [SuccessX]
pi@RPi4B:~/nim-dev/examples $ ls -l onemil3
-rwxr-xr-x 1 pi pi 57548 Dec  1 21:21 onemil3
pi@RPi4B:~/nim-dev/examples $ /usr/bin/time ./onemil3
r Len: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.06user 0.05system 0:00.12elapsed 99%CPU (0avgtext+0avgdata 10048maxresident)k
0inputs+0outputs (0major+2294minor)pagefaults 0swaps
pi@RPi4B:~/nim-dev/examples $

« Last Edit: December 01, 2019, 09:22:49 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #32 on: December 02, 2019, 05:43:01 PM »
Okay, one more, using JADE
Code: C++
  1. #include <jade.hpp>
  2.  
  3. MAIN
  4.     STRING s,t,r;
  5.     ARRAY<int> a;
  6.  
  7.     FOR( VAR x = 0 TO x<1000001 STEP x++ ) DO
  8.         a.push_back(x);
  9.         s += (CHAR)(x%26)+65;
  10.  
  11.         IF ( LEN(s) == 26 ) THEN
  12.             t += s;
  13.             s = "";
  14.         END
  15.     END
  16.  
  17.     r = REVERSE$(t);
  18.  
  19.     PRINT("r LEN: ",LEN(t));
  20.     PRINT("Front: ", LEFT$(r,26));
  21.     PRINT("Back:  ", RIGHT$(r,26));
  22.     PRINT("UBVal: ",a[1000000]);
  23. END

On my RPI 3B+:

riveraa@dpi:~/Projects/jade $ g++ -std=c++11 mil.cpp -ljade -o mil
riveraa@dpi:~/Projects/jade $ time ./mil
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000

real    0m0.194s
user    0m0.165s
sys    0m0.029s


AIR.

JadeLib Repository

EDIT:  Compiled with -O3, this is what I get on my RPI:

riveraa@dpi:~/Projects/jade $ g++ -O3 -std=c++11 mil.cpp -ljade -o mil
riveraa@dpi:~/Projects/jade $ time ./mil
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000

real    0m0.070s
user    0m0.060s
sys    0m0.010s



« Last Edit: December 02, 2019, 06:06:35 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #33 on: December 02, 2019, 06:26:35 PM »
Nice!

I'll setup a child board for JADE on the Raspberry BASIC forum.

When it comes to strings, C++ has a definite advantage.
« Last Edit: December 02, 2019, 06:28:50 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #34 on: December 02, 2019, 08:22:23 PM »
AIR,

I get the following errors trying to make JADE.


pi@RPi4B:~/jade-dev/jadelib $ make
Creating regex.o
Creating ltrim.o
Creating rtrim.o
Creating lcase.o
Creating instr.o
Creating grab.o
Creating split.o
Creating right.o
Creating repeat.o
Creating replace.o
Creating pad.o
bin.cc: In function ‘STRING BIN$(long int)’:
bin.cc:4:30: error: ‘CHAR_BIT’ was not declared in this scope
     std::bitset< sizeof(num)*CHAR_BIT > bits( num );
                              ^~~~~~~~
bin.cc:4:30: note: ‘CHAR_BIT’ is defined in header ‘<climits>’; did you forget to ‘#include <climits>’?
bin.cc:2:1:
+#include <climits>
 
bin.cc:4:30:
     std::bitset< sizeof(num)*CHAR_BIT > bits( num );
                              ^~~~~~~~
bin.cc:4:39: error: template argument 1 is invalid
     std::bitset< sizeof(num)*CHAR_BIT > bits( num );
                                       ^
bin.cc:5:17: error: request for member ‘to_string’ in ‘bits’, which is of non-class type ‘int’
     RETURN bits.to_string();
                 ^~~~~~~~~
make: *** [Makefile:38: bin.o] Error 1
pi@RPi4B:~/jade-dev/jadelib $


Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #35 on: December 02, 2019, 08:33:47 PM »
Do:

make clean

git pull

make

I added the missing header file to the bin.cc file.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #36 on: December 02, 2019, 09:24:30 PM »
That worked. Posted on RB forum.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #37 on: December 03, 2019, 08:37:40 AM »
Updated C version:

Code: C
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *strrev(char *str)
  6. {
  7.     if (!str || ! *str)
  8.         return str;
  9.  
  10.     int i = strlen(str) - 1, j = 0;
  11.  
  12.     char ch;
  13.     while (i > j) {
  14.         ch = str[i];
  15.         str[i] = str[j];
  16.         str[j] = ch;
  17.         i--;
  18.         j++;
  19.     }
  20.     return str;
  21. }
  22.  
  23.  
  24. int main() {
  25.     int a[1000001] = {0};
  26.     char *t=NULL;
  27.     char tmp[2] = {0};
  28.     char s[27] = {0};
  29.     FILE *t_stream;
  30.     size_t len;
  31.  
  32.     t_stream = open_memstream(&t, &len);
  33.  
  34.     for (int x = 0; x < 1000001; x++) {
  35.         a[x] = x;
  36.         tmp[0] = (char)(x%26)+65;
  37.  
  38.         strncat(s,tmp,2);
  39.  
  40.         if (strlen(s) == 26) {
  41.             fprintf(t_stream, "%s",s);
  42.             s[0] = '\0';
  43.         }
  44.  
  45.     }
  46.  
  47.     fclose(t_stream);
  48.  
  49.     strrev(t);
  50.  
  51.     printf("r LEN: %lu\n",strlen(t));
  52.     printf("Front: %.*s\n", 26, t);
  53.     printf("Back:  %s\n", t + strlen(t) - 26);
  54.     printf("UBVal: %d\n",a[1000000]);
  55.  
  56.     free(t);
  57.  
  58. }
  59.  

On my RPI 3B+

riveraa@dpi:~/Projects/C/1mil3 $ gcc -O3 mil.c -o mil
riveraa@dpi:~/Projects/C/1mil3 $ time ./mil
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000

real    0m0.201s
user    0m0.190s
sys    0m0.011s


AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #38 on: December 03, 2019, 09:35:02 AM »
Thanks AIR for the C update. C sometimes needs prosthetics to compete.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #39 on: December 03, 2019, 02:12:06 PM »
Thanks AIR for the C update. C sometimes needs prosthetics to compete.


...and swift needs a full body replacement....

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #40 on: December 03, 2019, 02:23:24 PM »
I'm curious to hear what the Swift maintainer for ARM has to say about my findings.

I'm still willing to give Swift a chance. It's a late comer to open source but seems to have a lot of interest by the industry. I like the transparent wide character / Unicode support, It seems swift on other platforms.
« Last Edit: December 03, 2019, 02:25:19 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #41 on: December 03, 2019, 06:45:01 PM »
AIR,

Can you put a Bash 1mil3 together while I work on the Java version? That will finalize the language set and I can create the result table. It would be great if Mike has time to contribute a Scheme version.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #42 on: December 03, 2019, 08:45:55 PM »
Bash version is not worth the effort, would take far too long to complete.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #43 on: December 03, 2019, 09:10:06 PM »
Okay.

The Java version is posted on the  RaspberryBASIC forum.
« Last Edit: December 03, 2019, 10:44:52 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #44 on: December 04, 2019, 08:40:38 AM »
Swift maintainer response.

Quote
Yes, that let r = String(t.reversed()) in particular is extremely slow. That reversed() is the generic reversal defined on Collections here, and appears to be executed lazily (has an internal _base String that points to the original t, but the actual reversal is an O(n) swap).
But what adds 5 minutes or so of execution time is the String(Collection) initializer , as we can see running this modified sample using an Array:

It should be an O(n) operation with some additional conversions performed on all 32-bit platforms, but clearly there is something more going on.

Corner case as it may be, I'd open a ticket on https://bugs.swift.org/.

I can't determine if this is a 32 bit or ARM issue. In either case Swift on the RPi is a dead language for me until it's fixed.
« Last Edit: December 04, 2019, 11:01:58 AM by John »