Classic Computer Magazine Archive COMPUTE! ISSUE 47 / APRIL 1984 / PAGE 68

THE BEGINNER'S PAGE

Richard Mansfield, Senior Editor

Size, Speed, Or Clarity

As you progress as a programmer, you'll start to notice that there are several ways to write a given program. Sometimes dozens of ways. Is it better to have the program use up as little memory as possible? Should it run at maximum speed? Or should you include extensive REMarks and subroutines to make the program LISTing easy to read and understand?

The following three programs all accomplish the same thing. They print the words one two three four five 100 times on screen. However, there are significant differences. They use up varying amounts of the computer's RAM memory; they run at different speeds; and they are of differing degrees of comprehensibility. This program emphasizes clarity:

Program 1: Clarity

10 TI$ = "000000" : REM TIMER SET AT ZERO
20 DATA ONE, TWO, THREE, FOUR, FIVE : REM ITEMS
    TO BE PRINTED
30 FOR I = 1TO5 : REM START OF LOOP
40 GOSUB 400 : REM SUBROUTINE FOR READ AND
   {SPACE}PRINT
50 NEXT : REM REDO LOOP
60 RESTORE : REM RESET DATA POINTER TO FIRS
   T ITEM
70 X = X + 1 : REM RAISE COUNTER
80 PRINT : REM PRINT CARRIAGE RETURN
90 IFX<100THEN30 : REM CHECK TO SEE IF 100
   {SPACE}IS REACHED
100 PRINTTI$ : REM PRINT ELAPSED TIME
110 END : REM FINISH OF PROGRAM
120 REM------------------------
400 REM SUBROUTINE TO READ AND PRINT DATA
410 READ D$ : REM GET DATA ITEM
420 PRINTD$ " " ; : REM PRINT DATA ITEM
430 RETURN

This version includes extensive REMark statements to define the purpose of each action. It also has a subroutine between lines 400–430. A subroutine is a portion of a program which is used more than once and is set apart from the main program. The subroutine in this example first READs, then PRINTs one of the words we're putting on screen, and then it RETURNs to the main program. The subroutine is set apart from the main program visually by the REM in line 120. Putting a subroutine off by itself like this contributes to the readability of the program's LISTing.

REMs Have Disadvantages

REMarks have no effect on what a program actually does. They do slow it down slightly when it RUNs, and they take up extra space in RAM memory. However, REMs make reading a program LISTing easier. You might want to modify your program at a later date and might then find the REMarks helpful when trying to follow the logic of the programming. Likewise, if someone else is trying to understand your program, REMarks can be of great assistance.

The TI$ in lines 10 and 100 is specific to Commodore computers. It first clears and then prints out the computer's internal clock. We've included it so that we can easily see how fast these different versions RUN.

But let's go for brevity now. We'll strip off all the REMarks, put the subroutine back inside the main program, and see how this changes things:

Program 2: Brevity

10 TI$ = "000000"
20 DATA ONE, TWO, THREE, FOUR, FIVE
30 FORI = 1TO5 : READ D$ : PRINTD$" "; : NEXT : PRI
   NT : RESTORE : X = X + 1 : IFX<100THEN30
50 PRINTTI$

This version is far shorter than the first version: Program 1 uses up 507 bytes in RAM memory; this one uses only 104 bytes. It's easy to see that overly enthusiastic REMarking could use up too much memory. Each character in a REMark takes up one byte in RAM memory. If you are writing a long program on, say, a VIC computer with only 3583 bytes of RAM, you have to be conservative about REMarking. If you REMarked to the same degree as in Program 1, you'd have only about 700 bytes of RAM in which to write your actual program.

Maximizing Speed

Program 2 runs faster than Program 1: seven seconds versus twelve. In our little illustration program, this doesn't much matter. But if you are programming a game or writing an inventory program, for example, where you have to manipulate large amounts of data, speed can matter a great deal. Each REMark, each additional line number, each jump from a main program to a subroutine (and back) is one more thing for the computer to interpret during the execution of a program.

Here's the same program maximized for speed:

Program 3: Speed

10 TI$ = "000000"
20 PRINT"ONE TWO THREE FOUR FIVE"
30 X = X + 1 : IFX<100THEN20
40 PRINTTI$

We've eliminated many actions here: DATA reading, RESTOREing, multiple PRINTing, and FOR-NEXT looping. Execution speed is now 5 seconds. And in the act of reducing and simplifying the actions the computer must take during its RUN, we've shortened the program as well. It now uses up 79 bytes in RAM. Although maximizing speed will not always maximize brevity, it often does.

Which is the best program? They're each fine, they simply emphasize different things. While you're a novice, it's often helpful to comment your programs heavily. You'll be writing short practice programs anyway. Then, as you become more familiar with reading LISTings, you can restrict your REMarks to the less obvious things. And as you gain programming experience, you'll discover ways to improve execution speed and conserve memory too.