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

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #15 on: November 30, 2019, 01:46:34 PM »
Can you post the complete script?

Offline jalih

  • Advocate
  • Posts: 74
Re: RaspberryBASIC.org Forum
« Reply #16 on: November 30, 2019, 01:50:12 PM »
Can you post the complete script?

I think AIR ment:
Code: [Select]
# Python 3 - 1mil3.py

s = ""
t = ""
a = [None] * 1000001

for x in range(1000001):
  s += chr(((x) % 26) + 65)
  a[x] = x
  if len(s) == 26:
    t += s
    s = ""

r = "".join(reversed(t))

print("r LEN: {}".format(len(r)))
print("Front: {}".format(r[:26]))
print("Back:  {}".format(r[-26:]))
print("UBVal: {}".format(a[1000000]))

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #17 on: November 30, 2019, 02:15:25 PM »
That code returns the following.


pi@RPi4B:~/python-dev/examples $ /usr/bin/time python3 1mil3.py
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: None
1.92user 0.05system 0:01.97elapsed 99%CPU (0avgtext+0avgdata 32552maxresident)k
0inputs+0outputs (0major+7191minor)pagefaults 0swaps
pi@RPi4B:~/python-dev/examples $


Offline AIR

  • BASIC Developer
  • Posts: 740
Re: RaspberryBASIC.org Forum
« Reply #18 on: November 30, 2019, 03:30:12 PM »
something is not right on your end.

On my RPI 3B+

time python3 1mil3.py
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000

real    0m6.103s
user    0m6.042s
sys    0m0.060s



Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #19 on: November 30, 2019, 03:48:34 PM »
Seems I had some file cross corruption.

Works on Python 2 & 3 now.
« Last Edit: November 30, 2019, 03:50:24 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 740
Re: RaspberryBASIC.org Forum
« Reply #20 on: November 30, 2019, 09:34:25 PM »
Hi AIR,

Do you have time to slap together a C submission for the third round? Maybe some pointer magic would make it scream.
Code: C
  1. #define _GNU_SOURCE
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define join(write_to,  ...) {           \
  8.     char *tmp_string_for_extend = (write_to); \
  9.     asprintf(&(write_to), __VA_ARGS__);       \
  10.     free(tmp_string_for_extend);              \
  11. }
  12.  
  13. char *strrev(char *str)
  14. {
  15.     if (!str || ! *str)
  16.         return str;
  17.  
  18.     int i = strlen(str) - 1, j = 0;
  19.  
  20.     char ch;
  21.     while (i > j) {
  22.         ch = str[i];
  23.         str[i] = str[j];
  24.         str[j] = ch;
  25.         i--;
  26.         j++;
  27.     }
  28.     return str;
  29. }
  30.  
  31.  
  32. int main() {
  33.     int a[1000001] = {0};
  34.     int i;
  35.     char *s=NULL,*t=NULL;
  36.  
  37.     join(s,"");
  38.     join(t,"");
  39.  
  40.     for (int x = 0; x < 1000001; x++) {
  41.         a[x] = x;
  42.  
  43.         join(s,"%s%c",s,(x%26)+65);
  44.  
  45.         if (strlen(s) == 26) {
  46.             join(t,"%s%s",t,s);
  47.             join(s,"");
  48.         }
  49.     }
  50.  
  51.     free(s);
  52.     strrev(t);
  53.  
  54.     printf("r LEN: %lu\n",strlen(t));
  55.     printf("Front: %.*s\n", 26, t);
  56.     printf("Back: %s\n", t + strlen(t) - 26);
  57.     printf("UBVal: %d\n",a[1000000]);
  58.  
  59. }

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #21 on: November 30, 2019, 10:40:38 PM »
I'm working on the Swift round 3 submission and it is running really slow. I don't have the array part working yet but even compiled doesn't seem  to make  a big difference. Any suggestions or code would be appreciated.

Code: Text
  1. // Swift 5.1.2 - 1mil3.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[x] = x
  10.   if s.count == 26 {
  11.     t = t + s
  12.     s = ""
  13.   }
  14. }
  15.  
  16. let r = String(t.reversed())
  17.  
  18. print("r LEN: ", r.count)
  19. print("Front: \(r.prefix(26))")
  20. print("Back:  \(r.suffix(26))")
  21. // print("UBVal: ", a[100000])
  22.  


pi@RPi4B:~/swift-dev/examples $ /usr/bin/time ./main
r LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
338.87user 35.15system 6:14.07elapsed 99%CPU (0avgtext+0avgdata 10344maxresident)k
0inputs+0outputs (0major+4026645minor)pagefaults 0swaps
pi@RPi4B:~/swift-dev/examples $

« Last Edit: December 01, 2019, 07:12:08 AM by John »

Offline jalih

  • Advocate
  • Posts: 74
Re: RaspberryBASIC.org Forum
« Reply #22 on: November 30, 2019, 11:19:34 PM »
I'm working on the Swift round 3 submission and if is running really slow. I don't have the array part working yet but even compiled doesn't seem  to make  a big difference. Any suggestions or code would be appreciated.

Test what is the time without reversing the string. It took 8th a very long time to finish the task and the culprit was the s:rev not trying to optimize in case of ASCII string (It's already fixed for the next release.). It took over 30 minutes to finish...

I don't know if it's cheating or not but just use:
Code: [Select]
t.reversed()
Instead of:
Code: [Select]
String(t.reversed())

It's a lot faster just enumerating the characters from back to front without building a new collection for a reversed string. Some call it cheating, I call it smart thing to do!

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #23 on: December 01, 2019, 07:07:09 AM »
Great job on the C submission AIR!

I wasn't looking forward to having to do that one. My hero.

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #24 on: December 01, 2019, 09:31:26 AM »
It looks like string reverse is the problem with Swift.

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


pi@RPi4B:~/swift-dev/examples $ /usr/bin/time ./main
r LEN:  999986
Front: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Back:  ABCDEFGHIJKLMNOPQRSTUVWXYZ
35.55user 0.01system 0:35.59elapsed 99%CPU (0avgtext+0avgdata 8508maxresident)k
800inputs+0outputs (3major+918minor)pagefaults 0swaps
pi@RPi4B:~/swift-dev/examples $



@Jalih: I tried your t.reversed() idea but Swift complained.


pi@RPi4B:~/swift-dev/examples $ swiftc 1mil3.swift
1mil3.swift:21:7: error: cannot assign value of type 'ReversedCollection<String>' to type 'String'
r = t.reversed()
    ~~^~~~~~~~~~
pi@RPi4B:~/swift-dev/examples $
[/tt\

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #25 on: December 01, 2019, 11:51:33 AM »
AIR,

Here is the results of your C submission.


pi@RPi4B:~/c-dev/examples $ gcc -O3 1mil3.c -o 1mil3
pi@RPi4B:~/c-dev/examples $ ls -l 1mil3
-rwxr-xr-x 1 pi pi 8140 Dec  1 11:23 1mil3
pi@RPi4B:~/c-dev/examples $ /usr/bin/time ./1mil3
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back: ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
45.56user 63.56system 1:49.17elapsed 99%CPU (0avgtext+0avgdata 23956maxresident)k
0inputs+0outputs (0major+7535448minor)pagefaults 0swaps
pi@RPi4B:~/c-dev/examples $


Offline AIR

  • BASIC Developer
  • Posts: 740
Re: RaspberryBASIC.org Forum
« Reply #26 on: December 01, 2019, 01:01:50 PM »
AIR,

Here is the results of your C submission.


Yep, it's not the most optimized way of implementing this, but you didn't want predefined buffers.  This implementation creates a new buffer each time 'join' is called and concats the data, which is expensive.

BTW, your swift code compiles and runs fine on my Mac Mini.  There is a difference when compiled in debug mode (the default) and release mode.

[riveraa@mini ~/Projects/Swift/blah] $ time .build/x86_64-apple-macosx10.10/release/blah
r LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA

real    0m1.979s
user    0m1.846s
sys    0m0.130s

[riveraa@mini ~/Projects/Swift/blah] $ time .build/x86_64-apple-macosx10.10/debug/blah
r LEN:  999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA

real    0m2.678s
user    0m2.541s
sys    0m0.132s


For comparison, the C code executed on my Mac Mini:

r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back: ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000

real    0m2.554s
user    0m2.405s
sys    0m0.145s

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #27 on: December 01, 2019, 02:08:20 PM »
ScriptBasic is acting cooky about its 50 second run.

I wonder what FreeBasic is doing under the covers with its conversion to C? Damn fast.

Offline AIR

  • BASIC Developer
  • Posts: 740
Re: RaspberryBASIC.org Forum
« Reply #28 on: December 01, 2019, 03:41:55 PM »
Code: Go
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. // Reverse returns its argument string reversed rune-wise left to right.
  8. // From: https://github.com/golang/example/blob/master/stringutil/reverse.go
  9. func Reverse(s string) string {
  10.     r := []rune(s)
  11.     for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
  12.         r[i], r[j] = r[j], r[i]
  13.     }
  14.     return string(r)
  15. }
  16.  
  17. func main() {
  18.     var s = ""
  19.     var t = ""
  20.     var a [1000001]int
  21.  
  22.     for x := 0; x < 1000001; x++ {
  23.         a[x] = x
  24.  
  25.         s += string((x % 26) + 65)
  26.  
  27.         if len(s) == 26 {
  28.             t += s
  29.             s = ""
  30.         }
  31.     }
  32.  
  33.     t = Reverse(t)
  34.  
  35.     fmt.Println("r LEN: ", len(t))
  36.     fmt.Println("Front: ", t[0:26])
  37.     fmt.Println("Back:  ", t[len(t)-26:])
  38.     fmt.Println("UBVal: ", a[1000000])
  39.  
  40. }

Offline John

  • Forum Support / SB Dev
  • Posts: 2875
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #29 on: December 01, 2019, 03:53:14 PM »
Nice GOing!


pi@RPi4B:~/go-dev/examples $ go build 1mil3.go
pi@RPi4B:~/go-dev/examples $ /usr/bin/time ./1mil3
r LEN:  999986
Front:  ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:   ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal:  1000000
22.88user 4.71system 0:21.92elapsed 125%CPU (0avgtext+0avgdata 27036maxresident)k
0inputs+0outputs (0major+6656minor)pagefaults 0swaps
pi@RPi4B:~/go-dev/examples $