.op FASTER THAN BASIC =================== Ian Cull Bsc. 3/8/89. --------------------- Part 2. Hisofts Basic Compiler ------------------------------ Last month we discussed four ways of getting programs to run more quickly - tweaking the code, learning a new language, programming in assembly language, or using a compiler. Any program, written in any language (including assembler) can be tweaked, but the improvements generally are not phenomenal. We will look at tweaks later ... As an aside, you could try changing your Spectrum! The various versions of ZX Basic (16K, 48K, 128K and +3/+2A) all run at slightly different speeds - my Spectrum timings are (without the PRINT statement inside the loop) :- PRIMES1 PRIMES2 +3 Basic 39.2 60.0 48K Basic 32.4 41.3 Learning a new language is not a simple task, and so the best bet is to use a compiler. Compilers are available to compile many languages, but the best option is to use a Basic compiler since then there is, in theory, no learning needed and no rewriting of existing programs. Hisoft offer a very powerful Basic compiler, with versions for all Spectrums. The compiler, once loaded, occupies 11K of memory - although 128K versions store themselves in the RamDisk, so occupying very little main memory; they also support the PLAY command (and the resulting code can be run on 48K machines, where the PLAY code is simply ignored). All interaction with the compiler is via a number of commands; on 48K machines you type a * followed by a single letter (C for compile, etc). 128K machines have a menu called up by pressing TRUE VIDEO & INV VIDEO together; then the required letter is pressed. Hisoft Basic Compiler functions ------------------------------- *C - Compile the whole program, to machine code below RAMTOP; RAMTOP is moved down and code start address & size are given. *R - Run the latest compiled code (same as RANDOMIZE USR xxxxx). *X - Clear & Reset RAMTOP; this discards the compiled code (each new compilation takes up more memory). *T - Variables info; used to analyse an executing program. *ERASE - Basic program - leaves the compiler in memory. *D - Compile Data only - useful for very large programs. *E - Compile except Data. .pa Variable types -------------- The compiler supports normal floating point variables as well as integers (-32768 to 32767) and positive-only integers (0 to 65535) and strings, by default up to 255 characters in length. Special compiler instructions (e.g. REM : LEN A$ < 20) specify the maximum length of any individual string, as well as starting and stopping compilation, specifying which variables are to be integers, etc. An integer variable takes up less memory (2 bytes instead of 5) and can be processed much quicker than a normal one (leading to faster code). However, the accuracy of a normal variable is sometimes needed - examining the code to identify which variables can be integers is often difficult; so the compiler has a function (INFO) which allows the program to be run under ZX Basic (somewhat slower than normal) and a report obtained indicating what variables were used, and what type they can be. This mode also reports the maximum length of strings used - this is needed since strings must have a maximum length (255 unless otherwise specified). Most Spectrum Basic Compilers are very limited as to what code can be translated - that is how they get the code to run faster than Basic. The Hisoft compiler, however, acccepts almost any Basic program (not CLEAR, LOAD/SAVE, RUN and a few other commands that will cause few problems). Some programs need special treatment - for example, if any GOSUB or GOTO commands have computed line numbers (e.g. GOSUB 100*N) then the compiler can be told to produce slightly slower code rather than just giving up. Other limitations are that DATA statements cannot reference variables (DATA 100*3 is OK, DATA 100*X is not) - this allows the compiler to convert the DATA to a table of values once, instead of every time the program is run. Also, the compiler only supports only 1 and 2 dimensional arrays (you cannot have A(1,2,3) for example); and the VAL"..." function must be of the VAL"123" form, so VAL"N*N" cannot be processed (however, the 128K/+3 compiler has a way of dealing with this problem). Not Much Faster Than Basic? --------------------------- As a result of the power of this compiler, most programs can be compiled without any alteration but speed improvements will only be of the order of 2 to 5 times. This is reasonable, but could be much better. If the code is tweaked, making it more efficient once compiled, then improvements of 10 to 20 times (or more) are possible - the major benefit of this way of writing programs is that the code can be debugged in the normal way using ZX Basic then compiled once it is working fully. The amount of tweaking possible depends on the program (how many variables can be made integers, for example). .pa PRIME testing ------------- To see how fast the Hisoft Basic compiler code can be, I passed PRIMES1 & PRIMES2 through it. I used the +3 compiler for these tests, and removed the PRINT statements from inside the code (leaving only the 'Prime 100 is 541' message as output). In order to activate the compiler, I added the line 1 REM:OPEN # - REM: lines are commands to the compiler, and OPEN# is the command to begin compilation. Compilation is fast (less than 1.5 seconds) but is not automatic - messages are given during the early passes, and a key must be press to continue compilation. After compilation, PRIMES1 executed in 8.6 seconds (4.5 times quicker), and PRIMES2 executed in 5.2 seconds (11.5 times faster). Next, since both programs only use integer variables, I inserted the line 1 REM: INT+ P,PTOP,PCNT ... (after the INT+ you list all variables which are to be positive-only integers). This 'declaration command' has to preceed the OPEN#, which I moved to line 2. Now, PRIMES1 runs in 6.5 seconds (6 times FTB), but PRIMES2 manages an amazing 1.3 seconds (46 times FTB). This is to be expected, since PRIMES2 is actually a very simple program (the sieve method of calculating primes is renowned for its speed), and the poor show of ZX Basic is evidence of how slow it is. PRIMES1 has a lot of calculations (multiplies & divides) which are hard for the Z80 to do. Amazing though these results are, further tweaking is possible. Removing the (redundant) '<>0' from line 110 and compiling gives a time of 0.95 seconds (62 times FTB since only 0.5 seconds is saved in +3 Basic using this tweak)! Also replacing the code in lines 150 & 160 with a FOR...NEXT loop gives a compiled time of 0.85 seconds, although +3Basic improves to 42.8 seconds! Of course, there is more to compilation than speed - code size is also important. PRIMES1 results in 514 bytes of code (the Basic is 494 bytes), but PRIMES2 runs in 298 bytes (from 423 bytes of Basic). These are VERY good results (often compiled code has an overhead of 8K or more). Compilation speed, as mentioned already, is also quick. I compiled a small program which takes 18seconds to poke some code into memory - compilation took 2.6seconds and the resulting code executed in 2.2seconds, so compiling & running was still 3 times faster than the Basic (this doesn't include the loading time of the compiler, however). WARNING: These results for the PRIMES programs are particularly good - I chose them as examples deliberately. A large Basic program (GLOBALPLOT from Chezron Softwares OUTLET disk magazine, No.18) run in compiled form twice as fast as Basic, but still took 22 minutes! .pa Summary ------- The compiler comes with a good 46 page A5 manual, with a tutorial section and good appendices (but no index) and some example programs - a PACMAN style game (8 seconds to compile from 6150 bytes of Basic to 9635 bytes of code, including 1300 bytes to check for the BREAK key). A FROGGER style game offered similar improvements. The manual also discusses ways of tweaking programs and differences between ZX Basic and compiled code (which are few). The resulting machine code can be compiled to run at any address, and the compiler does not need to be loaded to run the code. In summary, Hisofts Basic Compiler is an excellent piece of software for those wanting to write faster than Basic programs without having to learn any new languages - a bonus is that the code is protected (it is almost impossible to take a compiled program and recreate the Basic). The only bad point is the price; #24.95 on tape or #29.95 for the improved Spectrum+3 version (which has additional commands for creating disk data files so is excellent for anyone making serious use of the +3). Contact Hisoft at The Old School, Greenfield, Bedford MK45 5DE (or telephone 0525 718181).