Copyright 1984 by ABComputing July 15, 1984 ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º *** Tips & Hints *** º º Towards Professional BASIC Programming º º º º by º º º º Joseph Juhasz º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Introduction ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This column is geared toward the more advanced BASIC programmer, looking for the final touches to polish a program's function and make it appear more professional. Beginners will also find the articles informative and useful. We will discuss topics ranging from combatting BASIC's garbage-collection delays to creating a very powerful and general data-entry routine to replace the BASIC INPUT command. As I develop many graphically based software products, several routines will be presented to improve and enhance your graphic-programming capability. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Tips and Hints ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ When writing programs that run under the BASIC interpreter, performance and memory usage can be a problem. A few tips and hints to improve performance are given below. ÚÄÄÄÄÄÄÄ¿ ³ Tip 1 ³ ÀÄÄÄÄÄÄÄÙ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Start each program with ³ ³ ³ ³ DEFINT A-Z ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This statement declares all variables to be stored as 2-byte integers, conserving memory and allowing most BASIC instructions to operate faster. To create other types of variables, simply use the type declaration characters ($,%,!,#). ÚÄÄÄÄÄÄÄ¿ ³ Tip 2 ³ ÀÄÄÄÄÄÄÄÙ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ To speed up [FOR ... NEXT] loops, omit ³ ³ the name of the counter variable in the ³ ³ NEXT statement. ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The statement FOR I=1 to 1000: NEXT can be significantly faster than FOR I=1 to 1000: NEXT I When using nested [FOR ... NEXT] loops it is still good practice to include the name of the counter variable with the NEXT statement. ÚÄÄÄÄÄÄÄ¿ ³ Tip 3 ³ ÀÄÄÄÄÄÄÄÙ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Define the most frequently used ³ ³ variables at the beginning of the ³ ³ program. ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ BASIC performs a sequential-table search each time a variable is referenced. Your programs can run significantly faster if the most heavily used variables are defined at the beginning of the program. With this in mind, one should try to reuse temporary variables, so that BASIC's internal table of variables does not grow too large and cause additional delays. I start each program with a statement similar to: I=0: J=0: K=0: TEMP=0: COUNTER=0: I$="": J$="": K$="" Then, when possible, I try to use these variables in all loops and for temporary storage. ÚÄÄÄÄÄÄÄ¿ ³ Tip 4 ³ ÀÄÄÄÄÄÄÄÙ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Use random-access data files. ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ I am uncertain why, but BASIC handles random-access data files much more efficiently than sequential-access data files. Thus, when determining the structure of your INPUT/OUTPUT/WORK files, if possible, use random access. The improvement in performance is much more than would be expected. ÚÄÄÄÄÄÄÄ¿ ³ Tip 5 ³ ÀÄÄÄÄÄÄÄÙ The following PEEK can be used to determine if your program is running on a PC or a PCjr: DEF SEG=&HFFFF: PCJR.SW = (PEEK(&HE)=&HDF): DEF SEG If running on a PCjr, then PCJR.SW will be true; otherwise, false. The switch can be tested by the following statement: IF PCJR.SW THEN PRINT "On a PCjr" ELSE PRINT "On a PC" ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ About The Author ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ Joseph Juhasz is founder and president of PCsoftware and has ³ ³ authored or co-authored many commercial products for the IBM ³ ³ PC including: PCcrayon, Executive Picture Show, CREATABASE, ³ ³ and Championship Blackjack. ³ ³ ³ ³ Joe can be reached at PCsoftware, 9120 Gramercy Drive, Suite ³ ³ #416, San Diego, CA 92123 ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ File Name: ÛÛ basic1.txt ÛÛ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ