Raspberry BASIC

Author Topic: Just REM  (Read 625 times)

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #15 on: November 25, 2019, 03:44:24 AM »
Perl

Code: Perl
  1. # Perl - 1mil.pl
  2.  
  3. my $accum = 0;
  4. my $milvar = 1000000;
  5.  
  6. for (my $i=1; $i <= 1000000; $i++) {
  7.   $accum++;
  8.   $milvar--;
  9.   if($accum == $milvar){
  10.     printf("Middle\n");
  11.   }
  12. }
  13. printf("Plus:  %d\n", $accum);
  14. printf("Minus: %d\n", $milvar);
  15.  


pi@RPi4B:~/perl-dev/examples $ /usr/bin/time perl 1mil.pl
Middle
Plus:  1000000
Minus: 0
0.33user 0.01system 0:00.34elapsed 99%CPU (0avgtext+0avgdata 3976maxresident)k
24inputs+0outputs (1major+186minor)pagefaults 0swaps
pi@RPi4B:~/perl-dev/examples $

« Last Edit: November 26, 2019, 07:45:10 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #16 on: November 25, 2019, 04:51:17 AM »
Python 3

Code: Python
  1. # Python 3 - 1mil.py
  2.  
  3. accum = 0
  4. milvar = 1000000
  5.  
  6. for x in range(1000000):
  7.   accum += 1
  8.   milvar -= 1
  9.   if accum == milvar:
  10.     print("Middle")
  11.  
  12. print("Plus:  {}".format(accum))
  13. print("Minus: {}".format(milvar))
  14.  


pi@RPi4B:~/python-dev/examples $ /usr/bin/time python3 1mil.py
Middle
Plus:  1000000
Minus: 0
0.73user 0.05system 0:00.78elapsed 98%CPU (0avgtext+0avgdata 7208maxresident)k
0inputs+0outputs (0major+728minor)pagefaults 0swaps
pi@RPi4B:~/python-dev/examples $

« Last Edit: November 26, 2019, 07:45:42 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #17 on: November 25, 2019, 08:12:03 PM »
Bash

It seems Bash doesn't like spaces with assignments using declare.

Code: Bash
  1. # bash - 1mil.sh
  2.  
  3. declare -i accum=0
  4. declare -i milvar=1000000
  5.  
  6. for i in {1..1000000}
  7. do
  8.   ((++accum))
  9.   ((--milvar))
  10.   if (( $accum == $milvar ));
  11.     then
  12.       echo "Middle"
  13.   fi
  14. done
  15.  
  16. echo "Plus:  " $accum
  17. echo "Minus: " $milvar
  18.  


pi@RPi4B:~/bash-dev/examples $ /usr/bin/time bash 1mil.sh
Middle
Plus:   1000000
Minus:  0
22.69user 0.48system 0:23.20elapsed 99%CPU (0avgtext+0avgdata 197804maxresident)k
8inputs+0outputs (0major+49929minor)pagefaults 0swaps
pi@RPi4B:~/bash-dev/examples $

« Last Edit: November 30, 2019, 04:40:21 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #18 on: November 25, 2019, 08:53:46 PM »
Java

Code: Java
  1. class onemil
  2.   {
  3.   public static void main(String args[])
  4.   {
  5.   int accum = 0;
  6.   int milvar = 1000000;
  7.   for (int i = 1; i <= 1000000; i++) {
  8.     ++accum;
  9.     --milvar;
  10.     if(accum == milvar){  
  11.       System.out.println("Middle");  
  12.     }  
  13.   }
  14.   System.out.println("Plus:  " + accum);
  15.   System.out.println("Minus: " + milvar);
  16.   }
  17.   }
  18.  


pi@RPi4B:~/java-dev/examples $ javac 1mil.java
pi@RPi4B:~/java-dev/examples $ ls -l onemil.class
-rw-r--r-- 1 pi pi 1001 Nov 25 22:48 onemil.class
pi@RPi4B:~/java-dev/examples $ /usr/bin/time java onemil
Middle
Plus:  1000000
Minus: 0
0.41user 0.03system 0:00.42elapsed 104%CPU (0avgtext+0avgdata       maxresident)k
0inputs+64outputs (0major+2234minor)pagefaults 0swaps
pi@RPi4B:~/java-dev/examples $

« Last Edit: November 26, 2019, 07:47:02 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #19 on: November 25, 2019, 10:24:42 PM »
JavaScript

Code: Javascript
  1. // JavaScript - 1mil.js
  2.  
  3. var accum = 0;
  4. var milvar = 1000000;
  5. var i ;
  6.  
  7. for (i = 1; i <= 1000000; i++) {
  8.   accum += 1;
  9.   milvar -= 1;
  10.   if ((accum == milvar)) {
  11.     console.log("Middle");
  12.   }
  13. }
  14.  
  15. console.log("Plus:  " + accum)
  16. console.log("Minus: " + milvar)
  17.  


pi@RPi4B:~/javascript-dev/examples $ /usr/bin/time node 1mil.js
Middle
Plus:  1000000
Minus: 0
1.65user 0.08system 0:01.74elapsed 99%CPU (0avgtext+0avgdata 30412maxresident)k
0inputs+0outputs (0major+6197minor)pagefaults 0swaps
pi@RPi4B:~/javascript-dev/examples $

« Last Edit: November 26, 2019, 07:47:30 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #20 on: November 26, 2019, 12:43:38 AM »
PHP

Code: PHP
  1. <?php
  2.  
  3. # PHP - 1mil.php
  4.  
  5. $accum = 0;
  6. $milvar = 1000000;
  7.  
  8. for ($i = 1; $i <= 1000000; $i++) {
  9.   $accum++;
  10.   $milvar--;
  11.   if ($accum == $milvar) {
  12.     echo "Middle\n";
  13.   }
  14. }
  15.  
  16. echo "Plus:  ", $accum, "\n";
  17. echo "Minus: ", $milvar, "\n";
  18.  
  19. ?>
  20.  


pi@RPi4B:~/php-dev/examples $ /usr/bin/time php 1mil.php
Middle
Plus:  1000000
Minus: 0
0.21user 0.01system 0:00.22elapsed 99%CPU (0avgtext+0avgdata 13140maxresident)k
0inputs+0outputs (0major+866minor)pagefaults 0swaps
pi@RPi4B:~/php-dev/examples $

« Last Edit: November 26, 2019, 07:48:05 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #21 on: November 26, 2019, 01:14:40 AM »
Swift

I may have found a bug with swiftc. The compiled version doesn't do the compare correctly and Middle is never printed.

Code: Text
  1. // Swift - 1mil.swift
  2.  
  3. var accum = 0
  4. var milvar = 1000000
  5.  
  6. for _ in 1...1000000 {
  7.   accum += 1
  8.   milvar -= 1
  9.   if accum == milvar {
  10.     print("Middle")
  11.   }  
  12. }
  13.  
  14. print("Plus:  ", accum)
  15. print("Minus: ", milvar)
  16.  

Interpretive

pi@RPi4B:~/swift-dev/examples $ /usr/bin/time swift 1mil.swift
Middle
Plus:   1000000
Minus:  0
0.90user 0.12system 0:02.20elapsed 47%CPU (0avgtext+0avgdata 86000maxresident)k
83968inputs+392outputs (368major+4659minor)pagefaults 0swaps
pi@RPi4B:~/swift-dev/examples $


Compiled

pi@RPi4B:~/swift-dev/examples $ swiftc 1mil.swift -o 1mil
pi@RPi4B:~/swift-dev/examples $ ls -l
total 36
-rwxr-xr-x 1 pi pi 14360 Nov 30 11:31 1mil
-rw-r--r-- 1 pi pi   211 Nov 26 01:28 1mil.swift
-rwxr-xr-x 1 pi pi 14524 Nov 26 01:29 main
pi@RPi4B:~/swift-dev/examples $ /usr/bin/time ./1mil
Middle
Plus:   1000000
Minus:  0
0.33user 0.01system 0:00.34elapsed 99%CPU (0avgtext+0avgdata 6724maxresident)k
0inputs+0outputs (0major+274minor)pagefaults 0swaps
pi@RPi4B:~/swift-dev/examples $ swift --version
Swift version 5.1 (swift-5.1.2-RELEASE)
Target: armv7-unknown-linux-gnueabihf
pi@RPi4B:~/swift-dev/examples $

« Last Edit: November 30, 2019, 07:37:30 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #22 on: November 26, 2019, 01:27:50 AM »
Here are the 1mil challenge results.
« Last Edit: November 28, 2019, 07:16:24 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #23 on: November 26, 2019, 09:32:09 PM »
AIR submitted an updated Bash script which is more efficient. (Thank You!) The post and the chart have been updated,

I have no plans to install Ruby or Rust. If you would like to submit an entry for these languages then either join the forum and post it or send me an e-mail to support @ raspberrybasic.org.
« Last Edit: November 26, 2019, 11:32:33 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #24 on: November 27, 2019, 03:11:46 AM »
Ruby

I wasn't aware that Ruby comes pre-installed.

Code: Ruby
  1. # Ruby - 1mil.rb
  2.  
  3. accum = 0
  4. milvar = 1000000
  5.  
  6. for x in 1..1000000
  7.   accum += 1
  8.   milvar -= 1
  9.   if accum == milvar then
  10.     puts "Middle"
  11.   end
  12. end
  13.  
  14. puts "Plus:  #{accum}"
  15. puts "Minus: #{milvar}"
  16.  


pi@RPi4B:~/ruby-dev/examples $ /usr/bin/time ruby 1mil.rb
Middle
Plus:  1000000
Minus: 0
0.42user 0.04system 0:00.57elapsed 81%CPU (0avgtext+0avgdata 6840maxresident)k
3008inputs+0outputs (1major+881minor)pagefaults 0swaps
pi@RPi4B:~/ruby-dev/examples $

« Last Edit: November 27, 2019, 03:28:18 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #25 on: November 28, 2019, 05:03:24 AM »
I 'm going to take this challenge one more round and add a string and array  to the mix.

FYI:  This site is a forum and not my blog. Without contributing members it will soon become dormant. It only takes a minute to register. It's okay to be a member of multiple forums and I'm pretty sure the RPi moderators won't ban you if you appear here.
« Last Edit: November 28, 2019, 05:44:40 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #26 on: November 28, 2019, 07:28:13 AM »
ScriptBasic

The array part takes 4 seconds of the total time.,

Code: Script BASIC
  1. ' ScriptBasic - 1mil3.sb
  2.  
  3. s = ""
  4. t = ""
  5. SPLITA STRING(1000001,"0") BY "" TO a
  6.  
  7. FOR x = 1 TO 1000000
  8.   s &= CHR(((x - 1) % 26) + 65)
  9.   a[x] = x
  10.   IF LEN(s) = 26 THEN
  11.     t &= s
  12.     s = ""
  13.   END IF
  14. NEXT
  15. r = STRREVERSE(t)
  16. PRINT "r LEN: ",LEN(r),"\n"
  17. PRINT "Front: ",LEFT(r, 26),"\n"
  18. PRINT "Back:  ",RIGHT(r, 26),"\n"
  19. PRINT "UBVal: ",a[1000000],"\n"
  20.  


pi@RPi4B:~/sbrt/examples $ /usr/bin/time scriba 1mil3.sb
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
15.64user 34.88system 0:50.55elapsed 99%CPU (0avgtext+0avgdata 172504maxresident)k
536inputs+0outputs (2major+4122825minor)pagefaults 0swaps
pi@RPi4B:~/sbrt/examples $

« Last Edit: December 03, 2019, 06:55:52 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #27 on: November 28, 2019, 07:11:56 PM »
8th  (Jalih submission)

Code: Text
  1. : plus-minus
  2.   n:1+
  3.   swap
  4.   n:1-
  5.   swap
  6.   2dup n:= if
  7.     "Middle\n" .
  8.   then ;
  9.  
  10. : app:main
  11.   1000000 0
  12.   ' plus-minus 1000000 times
  13.   "Plus: %d\nMinus: %d\n" s:strfmt .
  14.   bye ;
  15.  


pi@RPi4B:~/8th-dev/examples $ /usr/bin/time ./mil
Middle
Plus: 1000000
Minus: 0
0.41user 0.03system 0:00.45elapsed 96%CPU (0avgtext+0avgdata 6944maxresident)k
0inputs+0outputs (0major+340minor)pagefaults 0swaps
pi@RPi4B:~/8th-dev/examples $

ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #28 on: November 28, 2019, 09:00:08 PM »
BaCon

Code: Text
  1. ' BaCon - 1mil3.bac
  2.  
  3. s$ = ""
  4. t$ = ""
  5. DECLARE a[1000001] TYPE NUMBER
  6.  
  7. FOR x = 1 TO 1000000
  8.   s$ = s$ & CHR$(MOD((x - 1), 26) + 65)
  9.   a[x] = x
  10.   IF LEN(s$) = 26 THEN
  11.     t$ = t$ & s$
  12.     s$ = ""
  13.   END IF
  14. NEXT
  15. r$ = REVERSE$(t$)
  16. PRINT "r LEN: ",LEN(r$)
  17. PRINT "Front: ",LEFT$(r$, 26)
  18. PRINT "Back:  ",RIGHT$(r$, 26)
  19. PRINT "UBVal: ",a[1000000]
  20.  


pi@RPi4B:~/bacon-dev/examples $ bacon 1mil3.bac
Converting '1mil3.bac'... done, 21 lines were processed in 0.053 seconds.
Compiling '1mil3.bac'... cc  -c 1mil3.bac.c
cc -o 1mil3 1mil3.bac.o -lbacon -L. -lm 
Done, program '1mil3' ready.
pi@RPi4B:~/bacon-dev/examples $ ls -l 1mil3
-rwxr-xr-x 1 pi pi 109628 Nov 29 20:32 1mil3
pi@RPi4B:~/bacon-dev/examples $ /usr/bin/time ./1mil3
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
12.99user 0.00system 0:13.02elapsed 99%CPU (0avgtext+0avgdata 9356maxresident)k
0inputs+0outputs (0major+2157minor)pagefaults 0swaps
pi@RPi4B:~/bacon-dev/examples $

« Last Edit: November 30, 2019, 04:42:27 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • **
  • Posts: 81
    • View Profile
    • ScriptBasic
Re: Just REM
« Reply #29 on: November 29, 2019, 12:43:11 AM »
FreeBasic

Code: FreeBasic
  1. ' FreeBasic - 1mil3.bas
  2.  
  3. DIM s AS STRING
  4. DIM t AS STRING
  5. DIM r AS STRING
  6. DIM SHARED a(1000001) AS INTEGER
  7. DIM x AS INTEGER
  8.  
  9. FOR x = 1 TO 1000000
  10.   s &= CHR(((x - 1) MOD 26) + 65)
  11.   a(x) += x
  12.   IF LEN(s) = 26 THEN
  13.     t &= s
  14.     s = ""
  15.   ENDIF
  16. NEXT
  17.  
  18. FOR x = LEN(t) TO 1 STEP -1
  19.   r &= MID(t,x,1)
  20. NEXT
  21.  
  22. PRINT "r LEN: " & LEN(r)
  23. PRINT "Front: " & LEFT(r, 26)
  24. PRINT "Back:  " & RIGHT(r, 26)
  25. PRINT "UBVal: " & a(1000000)
  26.  


pi@RPi4B:~/freebasic-dev/examples $ fbc 1mil3.bas
pi@RPi4B:~/freebasic-dev/examples $ ls -l 1mil3
-rwxr-xr-x 1 pi pi 30640 Nov 28 16:40 1mil3
pi@RPi4B:~/freebasic-dev/examples $ /usr/bin/time ./1mil3
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.53user 0.05system 0:00.60elapsed 96%CPU (0avgtext+0avgdata 7848maxresident)k
0inputs+0outputs (0major+2623minor)pagefaults 0swaps
pi@RPi4B:~/freebasic-dev/examples $

ScriptBasic Project Manager/Facilitator