home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 110_01 / vtest.c < prev    next >
Text File  |  1984-03-03  |  6KB  |  235 lines

  1. /*
  2.  
  3.     The following is a most extroadinary program.
  4.  
  5.  
  6.     Why??
  7.  
  8.     It's a software analogue to David and Goliath,  where an
  9. inexpensive S-100 home computer programmed in a high level language
  10. slays a giant** IBM mainframe written that most giant of all
  11. languages, Assembler.
  12.  
  13.     My purpose in disseminating these results is to prod people
  14. into considering the advantages of programming in a modern, rich,
  15. portabale, maintainable high level language such as C.
  16.  
  17.     You can't dismiss a high level language out of hand just
  18. because of slow execution speed!
  19.  
  20. ---------------------------------------------------------------------
  21.  
  22. TIMES:
  23.  
  24. TRS-80 BASIC        Z80    23470 SEC
  25.  
  26. ASSEMBLER        Z80     1370 SEC
  27.  
  28. FIFTH            Z80      365 SEC
  29.  
  30. OPTIMIZING        370/148       79 SEC
  31. PL/I
  32.  
  33. BAL            370/148       56 SEC
  34.  
  35. VAX-11 BASIC        11/780       58.2 CPU SEC
  36.     (I/O TIME NOT INCLUDED)
  37.  
  38. BDS C 1.42 + CP/M    CROMENCO ZPU 4 MHZ 56 SEC
  39.  
  40. BDS C 1.42 + CP/M        Z-89      127 sec
  41.  
  42. BDS C 1.43a    Godbout dual    8085       45.84 sec
  43.     (8085 running at 5 Mhz)
  44.  
  45. */
  46.  
  47. /*
  48.  * C version of Prime Nuber Prog #3 of 3
  49.  * From Page 7-21 of 1980 DEC VAX Technical Summary
  50.  * Originally from March 80 Byte Magazine
  51.  * Incredible execution times are at end of this file.
  52.  *
  53.  * Sorry to say, my translation is no more readable than the
  54.  * Basic version.
  55.  *
  56.  */
  57.  
  58. /* Translated to C by Chuck Forsberg WA7KGX 7/3/81 */
  59. #include "a:bdscio.h"
  60. int c,m,k,p;
  61. struct _buf out;
  62.  
  63. main()
  64. {
  65.     fcreat("B:primes3.tmp", out);
  66.     printf("PRIME3\n\007");
  67.     fprintf(out, "    %d    %d    %d\n", 1,2,3);
  68.     c=0; m=3;
  69. p80:
  70.     m += 2;
  71.     p = m>>1;
  72.     for(k=3; k<=p; k += k-1) {
  73.         if(m%k == 0)
  74.             goto p190;
  75.     }
  76.     if(++c & 07)
  77.         fprintf(out, "    %d", m);
  78.     else
  79.         fprintf(out, "    %d\n", m);
  80. p190:
  81.     if(m<10000)
  82.         goto p80;
  83.     fprintf(out, "\nC= %d\n", c);
  84.     printf("C= %d\n", c);
  85.     /*
  86.      * I'm cheating just like VAX Basic by excluding file
  87.      * open and closing times.
  88.      */
  89.     printf("\007Time's Up\n");
  90.     putc(CPMEOF, out);
  91.     fflush(out);
  92.     fclose(out);
  93. }
  94. /*    The VAX Basic program.
  95. 15 DECLARE INTEGER M,K;
  96. 20 OPEN "PRIMES3.TMP" FOR OUTPUT AS FILE #1
  97. 30 PRINT "PRIME3"+TIME$(0)
  98. 40 PRINT
  99. 50 PRINT #1, 1;2;3;
  100. 55 C=0
  101. 70 M=3
  102. 80 M=M+2
  103. 90 FOR K=3 TO M/2 STEP K-1
  104. 100  IF(INT(M/K)*K-M = 0 THEN 190
  105. 110 NEXT K
  106. 121 PRINT #1%, M;
  107. 122 C=C+1
  108. 190 IF M < 10000 THEN 80
  109. 195 PRINT #1%, "C=";C
  110. 196 PRINT "C=";C
  111. 199 T2=TIME(1%)
  112.     P$="DONE"+NUM1$(0.1*(T2-T1))+"CPU SEC"
  113. 200 PRINT P$
  114.     PRINT #1, P$
  115. 201 END
  116.  
  117.     Version modified for Microsoft BASIC-80 Dialect
  118.  
  119. 15 DEFINT C,I,J,K,M
  120. 30 PRINT "PRIMES3"
  121. 40 PRINT
  122. 50 PRINT 1;2;3
  123. 55 C=0
  124. 70 M=3
  125. 80 M=M+2
  126. 90 FOR K=3 TO M/2 STEP K-1
  127. 100 IF ( M MOD K ) = 0 THEN 190
  128. 110 NEXT K
  129. 121 PRINT M;
  130. 122 C=C+1
  131. 190 IF M < 10000 THEN 80
  132. 196 PRINT "C=";C
  133. 200 PRINT "DONE"
  134.  
  135.     The above program was stopped at 7 minutes, it had only
  136. reached about 3000, running on a 4Mhz ZPU with no wait states.
  137.  
  138.     FIFTH version of the above.  365 Seconds on 64k Z89
  139. (2 mHz Z80).
  140.  
  141. ( prime numbers benchmark ) PRIMARY DECIMAL
  142. : PBENCH ." Prime numbers benchmark" CR CR ." 1 2 3 "
  143.         0 ( C)   3 ( m )
  144.         BEGIN 2+ ( m=m+2)
  145.             TRUE ( flag) OVER 2/ 3 DO
  146.               OVER ( m) I MOD  ( m mod k)
  147.               IF ELSE NOT LEAVE ENDIF I 1- +LOOP ( step k-1)
  148.               IF DUP . SWAP 1+ SWAP ENDIF ( print prime, c=c+1)
  149.         DUP 10000 >= UNTIL
  150.         CR ." C=" DROP . CR ." Done" 7 0 PUT ;
  151.  
  152. ;S
  153.  
  154. */
  155.  
  156. /*
  157.  * BAL means Basic Assembly Language, used on IBM mainframes
  158.  * BDS C was timed on a S-100 system with Cromenco ZPU 4 Mhz,
  159.  * 4FDC, Persci 277 (Single Dens), Microbyte static RAM, and
  160.  * Tek 4012 at 9600 baud.  Timed by Microma Digital Chronograph
  161.  * and bell-activated right hand index finger.
  162.  * The Zenith Z-89 had 64k and twin 5 inch drives.
  163.  * Timing for the BDS C systems includes most of the i/o, except
  164.  * for the final buffer flush and file closing.  The VAX Basic
  165.  * (and, presumably, PDP-10 basic as well) do NOT include i/o
  166.  * time!
  167.  *
  168.  *
  169.  * Other benchmarks suggest that C on 16 bit CPU's is many
  170.  * times faster than BDS C (on 8080's or Z80's).
  171.  *
  172.  * DEC IBM BDS 11 TRS et al trademarks
  173.  */
  174. cuchleft!=CURLEFT) {
  175.         poke(cuaddr,cuchleft);
  176.         poke(cuaddr+1,cuchrght);
  177.         }
  178.     switch (x)
  179.         {
  180.     case 'h': left();    /* move cursor left */
  181.         break;
  182.     case 'k': right();    /* and right */
  183.         break;
  184.     case 'j': down();    /* or down */
  185.         break;
  186.     case 'u': up();        /* or up */
  187.         break;
  188.     case 'b': bright();    /* place dot at cursor position */
  189.         break;
  190.     case 'd': dim();    /* remove dot f/*
  191.     Binary to HEX-ASCII utility
  192.  
  193.     Makes a binary file into a HEX file, 32 bytes/block with
  194.     proper sumcheck byte at the end. The starting address for the
  195.     HEX file is settable; the input is assumed to be contiguous
  196.     locations. Some garbage may be included at the end of the
  197.     HEX file since the program does not recognize 0x1A as an
  198.     end-of-file code in the binary input.
  199.  
  200.     Called with:
  201.         bin2hex [<binary file 1> [<bin. file 2>...]]
  202.  
  203.     Nothing special required, but if you have NSECTS
  204.     set larger than 1 you may want to include the
  205.     modified "getc" for better end-of-file indication.
  206.  
  207. written by:
  208.     Robert Pasky
  209.     36 Wiswall Rd
  210.     Newton Centre, MA 01259
  211. */
  212.  
  213. #include "bdscio.h"
  214.  
  215. #define CTRLQ 0x11    /* quit key: control-q */
  216.  
  217. int i, j, fd;
  218. int addr,sad;
  219. char fnbuf[30], *fname;        /* filename buffer & ptr */
  220. char onbuf[30], *oname;        /* output filename buf & ptr */
  221. char ibuf[BUFSIZ];    /* buffered input buffer */
  222. char obuf[BUFSIZ];    /* buffered output */
  223. char hextab[16];
  224. char kybdbuf[20];
  225. char sumcheck;
  226.  
  227. main(argc,argv)
  228. char **argv;
  229. {
  230.     strcpy(hextab,"0123456789ABCDEF");
  231.  
  232.     while (1)
  233.     {
  234.         oname = onbuf;
  235.         if (argc