.op FASTER THAN BASIC =================== Ian Cull Bsc. 6/10/89. ---------------------- Part 3. MIRA Softwares Compilers -------------------------------- The best way to speed up your programs is to spend a long time learning assembly language and write your programs is machine code. Last months Basic Compiler is really the opposite extreme - you didn't need to learn anything new before being able to produce faster programs. This month we look at Compilers for languages other than Basic - specifically Fortran and Pascal from Mira Software. The MIRA Compilers are similar; both support both 48K and 128K Spectrums with tape, Sinclair Microdrives, Plus D, Beta and Opus disk interfaces and even Wafadrives (48K only - Wafadrives don't work on 128K Spectrums). The 128K versions also fully support the RamDisk. There are no versions for the Spectrum+3, and the Spectrum+2A is also likely to give problems. Fortran ------- Fortran is a very old language, originally designed so that scientists could write their own programs to solve problems instead of having to describe them to computer programmers. Fortran is quite easy for Basic programmers to pick up - its layout is similar (apart from needing 6 spaces at the start of every line) and offers named functions & subroutines (but only 6 character names). Fortrans has very powerful maths functions (Fortran is an abbreviation of Formula Translation); the MIRA compiler omits Double Precision and Complex variables, still offering REAL, INTEGER & LOGICAL. CHARACTER variables are very poorly supported - stick to Basic if you want to process strings. Maths functions include Logs (to base e or base 10) normal & hyperbolic trig. functions (SIN, COS, etc) & the Fortran MAX & MIN functions. The Fortran DO loop is very similar to Basics FOR/NEXT loop, and Fortran also offers block, logical & arithmetic IF statements - enough power for most programs. MIRA have also implemented COMMON & EQUIVALENCE, allowing sharing of variables between subroutines. Spectrum specific routines include CIRCLE, DRAW, PLOT, BEEP and USR. Fortrans input/output functions are also well implemented; there are many powerful facilities for formatting the results of your programs using the FORMAT command. Input/Output channels must, however, be set up from Basic before calling the code. Pascal ------ Pascal is a strictly defined language, designed originally for teaching purposes (by Nicklaus Wirth). It offers many facilities for structuring programs (making them easier to understand); the MIRA Compiler conforms to BS6192 level 1 so is no exception. A Basic programmer new to Pascal has little chance of success without buying additional books. Because of the teaching background there is little chance of programming errors not being spotted during compilation; however, learning where and where not to put semi-colons is a tedious process. Pascal offers a wide range of variable types including ordinals (integers), reals, characters and sets (a variable containing one or more items from a defined list). Pascal sets are particularly powerful, allowing you to restrict a variables values which aids debugging. Character processing is much better than Fortran - one of MIRAs example programs is a Spelling Checker! MIRA have again implemented Spectrum specific functions including all those available for Fortran. Also TAN, ARCSIN and ARCCOS are implemented, as well as being available in the Pascal definition - this is not clarified in the manual. Files are again implemented by OPENing them to streams in Basic - however 'internal files' can also be created and destroyed by the compiler (on disk versions only) as the program runs. Using the Compilers ------------------- The MIRA Compilers both have a rather unusual user interface. The program source is listed in a similar way to Basic (i.e. in the top screen) but without line numbers. The cursor keys can be used to move up and down the source 1 or 8 lines at a time, or to move to the top or bottom of the program. Pressing ENTER allows new text to be inserted after the current line (until CAPS-SHIFT 6 is pressed), and E allows entry of a single line before the current one. Pressing B or K mark the beginning and end of a block and 'cut it' out, then P allows the block to be 'Pasted' back as many time as necessary (to copy a block you simply re-paste it before moving the cursor). A block can also be deleted completely. There is a find function (F specifies the search text and begins the search, N continues through the text) and the source can be SAVEd or LOADed to tape, RamDisk (with the 128K version) or to any available drive (according to the version being run). LOADed text is always merged with what is already in memory - this must therefore first be deleted (using the memorable (!) sequence SYM-SHIFT 7 B SYM-SHIFT 6 CAPS-SHIFT 0). An 'option byte' allows setting of where the SAVE/LOAD commands are directed to (i.e. in a separate operation to the actual SAVE/LOAD). Compilation of loaded source is begun by pressing the X key; after a successful compilation both the source and the compiler are lost - 128K versions save to RamDisk first and 48K disk versions save to disk. Because of this I cannot recommend the 48K tape-only compilers. In addition, there is no access to Basic in the 48K versions once the compiler has been loaded (except a reset) so if a disk/cartridge is full, a new one cannot be formatted or files erased without losing the current source (though you could always save to tape!). I found a few bugs in the Fortran Compiler - IMPLICIT declarations (which save time by specifying all variable names starting with a particular letter to be a certain type) do not work, and using LOGICAL variables gives a lot of trouble - PRIMES2 is best written using a LOGICAL array but I could not get it to work. The 128K compiler also crashed when I tried to save a new version of a file to Plus D disk. There was also a (simple) error in the loader program which transfers the code to Wafadrive. The Pascal Compiler suffered from bugs too - whenever I finished running a compiled program on the 128K machine, it crashed. This did not occur on the 48K Wafadrive version. How Much Faster Than Basic? --------------------------- Both Fortran and Pascal were designed to be used with Compilers, so they should give good results. Pascal will in general be a little slower since it carries out much better erroe checking (Fortran would, for example, allow you to overwrite all memory by mistreating an array). As with all computer languages, the use of integers instead of reals where possible will give great improvement - both languages REQUIRE you to declare every variable as one particular type before using it. Compilation using the MIRA Compilers is, like Hisoft Basic, two pass. This is particularly noticable on the 48K Fortran compilers, as the second pass has to load in an additional code block - if an error is found during the second pass then you have to reload everything before fixing it! The Pascal Compiler has both passes in the one program. I rewrote PRIMES2 (without the displaying of intermediate results) in both Fortran and Pascal to give comparisons with Basic. The Fortran version took an incredible 0.57 seconds to run; this is over 100 times faster than +3Basic! The Pascal version took 1.5 seconds - indicating the overheads involved in strong error checking. Because the Compilers automatically save the source code before compilation (except the 48K tape-only versions), this slows compilation even further (especially noticable when I tried the Wafadrive version). This occurs EVEN if you have just saved the source yourself! Summary ------- Both the Fortran & Pascal compilers are available from MIRA Software for #15 each - the tape contains all 48K & 128K versions and a few example programs. However, you may have to budget for additional books since the 'manuals' supplied do not attempt to teach the languages. I cannot seriously recommend using the program on a 48K tape-only Spectrum, and because of the automatic saving of source code & lack of access to Basic the best environment for these Compilers is a 128K Spectrum. Both compilers are good value and offer Spectrum users the chance to learn alternative programming skills. I really think that MIRA should investigate a +3 and/or +2A version so that all possible Spectrum markets are covered. Contact MIRA Software at 24 Home Close, Kibworth, Leicestershire LE8 0JT PROGRAM PRIMES C PRIMES2 rewritten in Fortran INTEGER P(1000),PCNT,PTOP,PRIME DO 35 I=1,1000 35 P(I)=0 P(1)=1 PTOP=1 PCNT=1 100 IF (PCNT.GT.100) GOTO 200 IF (P(TOP).NE.0) THEN PTOP=PTOP+1 GOTO 100 ENDIF PRIME=PTOP WRITE (2,135) PCNT,PRIME 135 FORMAT(I3,'=',I3) PCNT=PCNT+1 DO 150 I=PRIME,1000,PRIME 150 P(I)=1 GOTO 100 200 CONTINUE END PROGRAM primes(input,output); { PRIMES2 rewritten in Pascal } CONST top=100; VAR i,j,ptop:INTEGER; p:PACKED ARRAY [1..1000] OF BOOLEAN; pcnt:0..top; BEGIN FOR i:=1 TO 1000 DO p[i]:=false; pcnt:=0; ptop:=1; p[ptop]:=true; WHILE pcnt