Raspberry BASIC

Author Topic: One Mil Dash  (Read 8899 times)

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
One Mil Dash
« on: November 24, 2019, 09:22:52 PM »
ScriptBasic

Code: Script BASIC
  1. ' ScriptBasic - 1mil.sb
  2.  
  3. accum = 0
  4. milvar = 1000000
  5.  
  6. FOR x = 1 TO 1000000
  7.   accum += 1
  8.   milvar -= 1
  9.   IF accum = milvar THEN PRINT "Middle\n"
  10. NEXT
  11. PRINT "Plus:  ",accum,"\n"
  12. PRINT "Minus: ",milvar,"\n"
  13.  


pi@RPi4B:~/sbrt/examples $ /usr/bin/time scriba 1mil.sb
Middle
Plus:  1000000
Minus: 0
2.18user 0.00system 0:02.21elapsed 99%CPU (0avgtext+0avgdata 1972maxresident)k
520inputs+0outputs (1major+108minor)pagefaults 0swaps
pi@RPi4B:~/sbrt/examples $
« Last Edit: November 26, 2019, 09:51:38 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #1 on: November 24, 2019, 11:15:02 PM »
BaCon

Code: Text
  1. ' BaCon - 1mil.bac
  2.  
  3. accum = 0
  4. milvar = 1000000
  5.  
  6. FOR x = 1 TO 1000000
  7.   accum = accum + 1
  8.   milvar = milvar -1
  9.   IF accum = milvar THEN PRINT "Middle"
  10. NEXT
  11. PRINT "Plus:  ",accum
  12. PRINT "Minus: ",milvar
  13.  


pi@RPi4B:~/bacon-dev/examples $ bacon 1mil.bac
Converting '1mil.bac'... done, 13 lines were processed in 0.035 seconds.
Compiling '1mil.bac'... cc  -c 1mil.bac.c
cc -o 1mil 1mil.bac.o -lbacon -L. -lm 
Done, program '1mil' ready.
pi@RPi4B:~/bacon-dev/examples $ ls -l 1mil
-rwxr-xr-x 1 pi pi 49968 Nov 25 21:03 1mil
pi@RPi4B:~/bacon-dev/examples $ /usr/bin/time ./1mil
Middle
Plus:  1000000
Minus: 0
0.03user 0.00system 0:00.03elapsed 96%CPU (0avgtext+0avgdata 1536maxresident)k
0inputs+0outputs (0major+84minor)pagefaults 0swaps
pi@RPi4B:~/bacon-dev/examples $

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

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #2 on: November 24, 2019, 11:40:30 PM »
FreeBasic

Code: FreeBasic
  1. ' FreeBasic - 1mil.bas
  2.  
  3. DIM accum AS Integer = 0
  4. DIM milvar AS Integer = 1000000
  5. DIM x AS Integer
  6.  
  7. FOR x = 1 TO 1000000
  8.   accum += 1
  9.   milvar -= 1
  10.   IF accum = milvar THEN PRINT "Middle"
  11. NEXT
  12. PRINT "Plus:  ",accum
  13. PRINT "Minus: ",milvar
  14.  


pi@RPi4B:~/freebasic-dev/examples $ fbc 1mil.bas
pi@RPi4B:~/freebasic-dev/examples $ ls -l 1mil
-rwxr-xr-x 1 pi pi 30092 Nov 25 21:13 1mil
pi@RPi4B:~/freebasic-dev/examples $ /usr/bin/time ./1mil
Middle
Plus:          1000000
Minus:         0
0.00user 0.01system 0:00.06elapsed 34%CPU (0avgtext+0avgdata 2072maxresident)k
0inputs+0outputs (0major+125minor)pagefaults 0swaps
pi@RPi4B:~/freebasic-dev/examples $

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

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #3 on: November 25, 2019, 12:37:07 AM »
C

Code: C
  1. // C - 1mil.c
  2.  
  3. #include <stdio.h>
  4.  
  5. int main (int argc, char** argv)
  6. {
  7. int accum = 0;
  8. int milvar = 1000000;
  9. int x;
  10.  
  11. for(x = 1; x <=  1000000; ++x)
  12.   {
  13.     ++ accum;
  14.     -- milvar;
  15.     if(accum == milvar) printf("Middle\n");
  16.   }
  17.   printf("Plus:  %d\n", accum);
  18.   printf("Minus: %d\n", milvar);
  19.   return 0;
  20. }
  21.  


pi@RPi4B:~/c-dev/examples $ gcc 1mil.c -o 1mil
pi@RPi4B:~/c-dev/examples $ ls -l 1mil
-rwxr-xr-x 1 pi pi 8016 Nov 25 21:24 1mil
pi@RPi4B:~/c-dev/examples $ /usr/bin/time ./1mil
Middle
Plus:  1000000
Minus: 0
0.02user 0.00system 0:00.02elapsed 100%CPU (0avgtext+0avgdata 1208maxresident)k
0inputs+0outputs (0major+73minor)pagefaults 0swaps
pi@RPi4B:~/c-dev/examples $

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

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #4 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #5 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #6 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #7 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #8 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #9 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #10 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #11 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #12 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: 234
    • View Profile
    • ScriptBasic
Re: One Mil Dash
« Reply #13 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