home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Basic / MAXONB32.DMS / in.adf / docs.lha / B-Hints&Tips < prev    next >
Encoding:
Text File  |  1994-10-31  |  5.9 KB  |  181 lines

  1. Hints and Tips
  2.  
  3.  
  4.  
  5. This chapter shows how you can get the most out
  6. of programs written with the MaxonBASIC
  7. compiler. It is not necessarily intended only
  8. for the advanced programmer.
  9.  
  10.  
  11. Using MaxonBASIC
  12.  
  13. Making a more efficient program requires
  14. knowledge of the features of MaxonBASIC. The
  15. following suggestions are intended to give you
  16. a firmer understanding of what can be done; we
  17. also hope that you will use this information as
  18. the basis for a more detailed exploration of
  19. MaxonBASIC.
  20.  
  21.  
  22. defint a-z
  23.  
  24. It is a good idea to have this line in your
  25. program, it makes the default variable type a
  26. short integer. The main benefit from using ints
  27. is speed. The source becomes more
  28. understandable when &, !, and # are used
  29. explicitly.
  30.  
  31.  
  32. rem $VARCHECKS
  33.  
  34. This forces variable checks on. The primary
  35. benefit of this is that you can avoid
  36. unnecessary bugs caused by undefined variables
  37. in sub-programs and functions. This is
  38. especially important when programming using the
  39. operating system include files.
  40.  
  41.  
  42. STATIC variables in SUBs and FNs
  43.  
  44. STATIC variables retain their values when a SUB
  45. or FUNCTION is exited and re-entered. There is
  46. a speed benefit in using STATICs as opposed to
  47. LOCALs: STATICs are only allocated once,
  48. whereas LOCALs must be allocated every time a
  49. SUB or FUNCTION is invoked. If your SUB or
  50. FUNCTION is recursive then LOCAL variables must
  51. be used.
  52.  
  53.  
  54. INCR and DECR
  55.  
  56. These two statements respectively increment and
  57. decrement the value of a variable by one. When
  58. used on array elements they are considerably
  59. faster than e.g. Arr(1)=Arr(1)+1.
  60.  
  61.  
  62. ==
  63.  
  64. The double-equals comparison operator has a
  65. different meaning depending on the type of
  66. value compared. When comparing strings, a case-
  67. independent comparison is made. It is
  68. considerably faster than using UCASE$ or LCASE$
  69. and then comparing, or doing something like IF
  70. fred$="A" or fred$="a" THENÉ When comparing
  71. numeric values, == is used as ´almost equals´;
  72. rounding errors can thus be avoided. There is a
  73. performance degradation when using == on
  74. numeric values. Note that this is a MAXON
  75. extension.
  76.  
  77.  
  78. ! as opposed to #
  79.  
  80. When using floating point maths, it is a good
  81. idea to know exactly how much accuracy is
  82. actually necessary. Single-precision is much
  83. faster than double-precision, due to the degree
  84. of accuracy required. If you want floating
  85. point, but do not need such a high degree of
  86. precision, single-precision is the variable
  87. type to use. The exception to this is if you
  88. have a maths co-processor fitted to your system
  89. where doubles may actually be faster than
  90. singles. The reason for this is that our double
  91. routines use the system libraries that will
  92. automatically use an FPU if installed whereas
  93. the single routines are software only.
  94.  
  95.  
  96. Use pre-tokenised files
  97.  
  98. If you are using our operating system include
  99. (.bh and .bc) files then it is well worth
  100. building a pre-tokenised file containing those
  101. include files that you are using - you will get
  102. much faster compilations as a result.
  103.  
  104.  
  105. VARPTR, SADD and PEEKtype
  106.  
  107. When using VARPTR and SADD, you must be very
  108. careful. The reason for this is quite simple:
  109. owing to the dynamic heap allocation and the
  110. blindingly-fast garbage collection of
  111. MaxonBASIC, strings are prone to move around
  112. without any prior notice. This is normally
  113. completely transparent to the user, but when
  114. using these functions it becomes a factor to be
  115. reckoned with. If you are going to use them,
  116. call the functions just before accessing the
  117. variable or array. In addition the address of
  118. any array will be invalid if a REDIM or ERASE
  119. statement has been executed since VARPTR was
  120. used to find the address.
  121. After using these functions, a PEEK or POKE is
  122. usually inflicted upon the data at the returned
  123. address. MaxonBASIC has faster varieties of
  124. PEEK and POKE and they are type-specific. The
  125. can be used to great effect e.g. when parsing a
  126. string.
  127. Let us suppose an entire file has been loaded
  128. into a single string using INPUT$; this is
  129. quite possible because strings in MaxonBASIC
  130. are only limited by available memory. SADD is
  131. called to determine where the string is. To go
  132. through the file at high speed, all you need is
  133. to remember your place in the string and PEEKB
  134. from the location that you need. Do not use
  135. PEEKW or PEEKL because strings can be on odd
  136. boundaries and an address exception will occur
  137. if you try to read a word or long from an odd
  138. address.
  139.  
  140.  
  141. Making Your Programs "No-Limits"
  142.  
  143. If you have been used to programming in more
  144. primitive versions of BASIC or in Pascal or C,
  145. avoiding arbitrary restrictions on the size and
  146. type of data that your program can manipulate
  147. can be hard work. For example, having a limit
  148. on the length of names that you can type into a
  149. business application can sometimes be very
  150. annoying. Similarly avoiding limits on the
  151. lengths of files and in line lengths can save
  152. you a lot of time, if say the file in question
  153. has odd end-of-line markers that mean that your
  154. program treats the whole file as one line.
  155. The following hints should help to avoid this
  156. sort of problem:
  157. When reading in or adding to arrays, have code
  158. to make the array larger if need be. In general
  159. adding a few elements at a time is not a good
  160. idea because the program may start spending all
  161. its time moving arrays. Normally it is a good
  162. idea to start off an array with the size as
  163. just larger than a typical requirement, but if
  164. an array is only used occasionally then the
  165. dimension can start off small. For example, if
  166. writing a cross-reference program for
  167. MaxonBASIC programs, it would probably a good
  168. idea to start by assuming that the number of
  169. line numbers is small (say 10) and then if the
  170. program turns out to be a horrible old-
  171. fashioned program with a line number on every
  172. line then the arrays used for holding them can
  173. be grown using REDIM PRESERVE at, say, 100
  174. elements at a time.
  175. When using byte or record numbers in files or
  176. strings use long integer variables (terminated
  177. with &). This should remove automatically many
  178. 32k or 64k limits on programs that are designed
  179. with 16-bit integers in mind.
  180.  
  181.