home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DDJ1289.ZIP / TONKIN.LST < prev   
File List  |  1989-10-30  |  2KB  |  90 lines

  1. _PDQ: LESS BAGGAGE, FASTER CODE_
  2. by Bruce W. Tonkin
  3.  
  4. [EXAMPLE 1]
  5.  
  6. DEFINT A-Z 
  7. DIM flags(8190) 
  8. CLS 
  9. PRINT "25 iterations" 
  10. x# = TIMER 
  11. FOR j = 1 TO 25 
  12.   count = 0 
  13.   FOR i = 0 TO 8190 
  14.     flags(i) = 1 
  15.   NEXT i 
  16.   FOR i = 0 TO 8190 
  17.     IF flags(i) THEN 
  18.       prime = i + i + 3 
  19.       k = i + prime 
  20.       WHILE k <= 8190 
  21.         flags(k) = 0 
  22.         k = k + prime 
  23.       WEND 
  24.       count = count + 1 
  25.     END IF 
  26.   NEXT i 
  27. NEXT j 
  28. xx# = TIMER 
  29. PRINT USING "#### primes in ##.### seconds";count;xx#-x# 
  30. END 
  31.  
  32.  
  33. [EXAMPLE 2]
  34.  
  35. rem $include: 'pdqdecl.bas' 
  36. DIM flags(8190) 
  37. CLS 
  38. PRINT "25 iterations" 
  39. start&=pdqtimer& 
  40. FOR j = 1 TO 25 
  41.   count = 0 
  42.   FOR i = 0 TO 8190 
  43.     flags(i) = 1 
  44.   NEXT i 
  45.   FOR i = 0 TO 8190 
  46.     IF flags(i) THEN 
  47.       prime = i + i + 3 
  48.       k = i + prime 
  49.       WHILE k <= 8190 
  50.         flags(k) = 0 
  51.         k = k + prime 
  52.       WEND 
  53.       count = count + 1 
  54.     END IF 
  55.   NEXT i 
  56. NEXT j 
  57. done&=pdqtimer& 
  58. tot&=(10*(done&-start&))\182  'convert timer ticks to seconds. 
  59. frac&=(10000&*(10*(done&-start&) mod 182))\182  'and decimal. 
  60. PRINT count;"primes in ";rtrim$(str$(tot&));".";frac&;"seconds." 
  61. END 
  62.  
  63.  
  64. [EXAMPLE 3]
  65.  
  66. defint a-z              'by default, all variables are integers 
  67. call initstr(a$,128)    'initialize a$ to 128 characters 
  68. call initstr(b$,8192)   'buffer for file is 8K bytes 
  69. a$=command$             'look at the command line 
  70. if rtrim$(a$)="" then   'no file named; give help. 
  71.   print"Syntax:" 
  72.   print"UCASE filename" 
  73.   print"The named file will be converted in place to upper-case." 
  74.   end 
  75.   end if 
  76. open rtrim$(a$) for binary as #1  'no random files in PDQ 
  77. size&=lof(1)       'file size 
  78. if size&=0 then close 1:kill rtrim$(a$):end      'no such file 
  79. where&=1           'start with the first byte 
  80. while where&<=size&     'as long as there are bytes to read 
  81.   remains&=size&-where&+1    'how much is left? 
  82.   if remains&<8192 then call setlength(b$,remains&) '< 8192 bytes 
  83.   get 1,where&,b$       'grab the next chunk to convert 
  84.   b$=ucase$(b$)         'upper-case it 
  85.   put 1,where&,b$       'write it back out 
  86.   where&=where&+len(b$) 'update the file position 
  87. wend 
  88. end 
  89.  
  90.