home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 211 / 211.d81 / t.strings < prev    next >
Encoding:
Text File  |  2002-01-01  |  7.0 KB  |  245 lines

  1. u
  2.          BYTES: STRING SPEED
  3.  
  4.            by Maurice Jones
  5.  
  6.     On LOADSTAR #210 I described how
  7. array variables and simple variables
  8. were stored. In the case of string
  9. variables only the name, length, and
  10. two pointers are stored. The pointers
  11. show where the string is stored. The
  12. actual strings are stored either in
  13. the code or at the top of BASIC RAM,
  14. extending downward. This latter method
  15. of storage leads to the topic of
  16. garbage collection.
  17.  
  18.     If the string appears in the code,
  19. the pointers will point into the code,
  20. and no new memory will be needed.
  21. Strings created by statements such as
  22. A$="This is a string" and strings read
  23. from data statements will be stored
  24. this way.
  25.  
  26.     Strings introduced into the
  27. program by GET, STR$, or by
  28. calculation, such as A$=C$+D$, are
  29. stored at the top of BASIC RAM. Each
  30. successive string is stored in the
  31. first free area containing enough room
  32. for the entire string. This process
  33. continues until the computer finds
  34. that the space between the last string
  35. and the top of arrays is too small for
  36. the current string.
  37.  
  38.     When this happens, the garbage
  39. collection routine attempts to
  40. reorganize the space above arrays.
  41.  
  42.     To see why it may be possible to
  43. reorganize the space without losing
  44. variables, consider a program which
  45. uses A$ and B$ over and over. Suppose
  46. that each is stored in high memory and
  47. each takes on these values in this
  48. order: A$ is"CAT", A$ is "BIRD", B$ is
  49. "NUMBER" and finally B$ is "LETTER."
  50. The top of memory will now contain:
  51.  
  52.          LETTERNUMBERBIRDCAT
  53.  
  54.     If the garbage collection routine
  55. is used, the computer will look at the
  56. pointers of all variables and select
  57. the one nearest the top of RAM. In
  58. this case, it is A$ and the pointers
  59. indicate the start of BIRD, the
  60. current value of A$. Since this is the
  61. highest address found, any string
  62. above here is no longer needed so the
  63. computer moves BIRD to the top and the
  64. memory now contains:
  65.  
  66.          LETTERNUMBERBIRBIRD
  67.  
  68.     The routine now looks through the
  69. entire list again, deciding which
  70. pointer points to next highest memory
  71. location. In our example there is only
  72. one string left, B$. The current value
  73. of B$ is LETTER, so this is moved to
  74. the next space after the new position
  75. of A$ and the memory looks like this:
  76.  
  77.          LETTERNUMLETTERBIRD
  78.  
  79.     The pointer which indicates the
  80. bottom of strings is now set to the
  81. space where the newly placed LETTER
  82. starts. The old values are not erased
  83. but will be overwritten by the next
  84. string.
  85.  
  86.     Simple enough, but if the number
  87. of variables is large, the process can
  88. be time consuming. Since the simple
  89. variables and the array variables may
  90. contain strings, all of these areas
  91. must be searched completely FOR EACH
  92. VARIABLE. If there are 100 variables,
  93. each will be looked at many times, and
  94. more time will be required to move
  95. each string.
  96.  
  97.     Notice that the routine works with
  98. pointers and length of string until
  99. the actual move is made. Even then,
  100. these numbers allow the string to be
  101. moved efficiently, since the start of
  102. the new location is easily calculated.
  103.  
  104.     This also means that the actual
  105. moves required only a fraction of the
  106. time that the search requires when the
  107. number of strings is fairly large.
  108.  
  109.     The length of strings has very
  110. little effect on the time required for
  111. garbage collection. The dominating
  112. factor is the number of strings stored
  113. above the end of arrays. When the
  114. search is made, variables stored in
  115. the code are passed over since they
  116. are never moved. Run the demo for
  117. evidence of these statements and read
  118. the text screens for more information.
  119.  
  120.  
  121.     That about does it for the summary
  122. of the study, so now I will close with
  123. some comments and some suggestions.
  124.  
  125.     This whole thing was prompted by
  126. Jeff Jones's BASICS article on
  127. LOADSTAR #57, and while a few of his
  128. remarks may need to be qualified, his
  129. main points are well proved. A few
  130. jiffies here and there are of no great
  131. import. A few jiffies in each part of
  132. a 1000 step loop are very important.
  133. More time is gained by using short
  134. code and efficient algorithms than by
  135. attempting to change multiply to
  136. divide or some other such doubtful
  137. scheme. Worry more about the big
  138. picture.
  139.  
  140.     I now believe that garbage
  141. collection has been badly handled in
  142. the literature. The usual thing has
  143. been to make some pronouncement
  144. without any evidence or explanation.
  145. My opinion is this: If the number of
  146. strings stored at the top of memory is
  147. relatively small, say less than 60,
  148. serious problems with garbage
  149. collection will only arise if
  150. available memory is small. The
  151. problem, then, is to strike a balance.
  152.  
  153.     I suspect there are many
  154. techniques that need to be explored
  155. and discussed, but I am not the one to
  156. do it. I had a lot of trouble
  157. designing programs to illustrate the
  158. idea. The modified BYTES #57 demo that
  159. I mentioned in Part I contained very
  160. long (about 140 characters) strings
  161. and was constantly generating more.
  162. Yet, garbage was only collected after
  163. about six hands and required about
  164. SEVEN JIFFIES.
  165.  
  166.     Emerson said, "Fear always springs
  167. from ignorance." Could it be that he
  168. was talking about fear of garbage
  169. collection?
  170.  
  171.     It seems obvious that the speed
  172. and memory usage are connected in many
  173. ways and that the two must be
  174. considered together. I like heavily
  175. documented programs, and this study
  176. shows that speed is not seriously
  177. affected by REM statements, unless
  178. space is a problem.
  179.  
  180.     In order to fully understand how
  181. BASIC uses memory, one needs much more
  182. detail than was possible here.  I
  183. strongly suggest that serious
  184. programmers must understand this and
  185. suggest the sources that I mentioned
  186. in Part I.
  187.  
  188.     Numbers and strings stored by
  189. arrays use less space and are more
  190. quickly located than those stored by
  191. simple variables. I believe that it is
  192. only a matter of habit that so many
  193. simple variables are found in the
  194. code. With a little attitude
  195. adjustment, we could see this change.
  196. I cannot see any difficulty in
  197. declaring ALL variables early in the
  198. program nor with declaring the most
  199. heavily used simple variables near the
  200. top of the list. A simple DIM
  201. statement does it all. There's nothing
  202. wrong with something like
  203.  
  204. 100 DIMA$,I,J,K,B$,C$,B,C,D,X,Y,Z
  205.  
  206. at the beginning of your program.
  207.  
  208.     My examination of code has shown
  209. that some people do a good job of
  210. keeping the number of variables small,
  211. whereas others seem to be afraid to
  212. reuse variables at all. It is not
  213. difficult to know when the value
  214. stored by a variable will not be
  215. needed again, so that the variable can
  216. be reused.
  217.  
  218.     Even though I spent hours waiting
  219. for long loops to run, hours designing
  220. and revising tests, hours reading and
  221. hours evaluating, this study has been
  222. gratifying. I believe that I have kept
  223. my opinions to a minimum and have
  224. proved those things that I have
  225. claimed.
  226.  
  227.     I wish that those who read this
  228. could learn as much as I did. If you
  229. want to learn something, try to figure
  230. out how to explain it to someone else.
  231.  
  232.  MJ
  233.  
  234.  
  235.  [DAVE'S ADDENDUM:] Computers are
  236. necessarily logical. Pure reason and
  237. scientific method -- as displayed by
  238. Maurice -- are the only answers to
  239. wild rumors and half-truths. This goes
  240. for operating system issues and
  241. viruses! Has [anyone] spent even a few
  242. moments trying to scope out why
  243. Windows hangs?
  244.  
  245.  
  246.