home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / WINER.ZIP / CHAP8-3.BAS < prev    next >
BASIC Source File  |  1992-05-13  |  3KB  |  117 lines

  1. '*********** CHAP8-3.BAS - demonstrates the Compare and Search functions
  2.  
  3. 'Copyright (c) 1992 Ethan Winer
  4.  
  5. DEFINT A-Z
  6. DECLARE FUNCTION Compare% (SEG Type1 AS ANY, SEG Type2 AS ANY, NumBytes)
  7. DECLARE FUNCTION Compare2% (SEG Type1 AS ANY, SEG Type2 AS ANY, NumBytes)
  8. DECLARE FUNCTION SearchType% (Array() AS ANY, Sought AS ANY)
  9. DECLARE FUNCTION SearchType2% (Array() AS ANY, Sought AS ANY)
  10. DECLARE FUNCTION SearchType3% (Array() AS ANY, Searched AS ANY)
  11.  
  12. CLS
  13. TYPE FLen                       'this lets us use SEG
  14.   LastName AS STRING * 15
  15. END TYPE
  16.  
  17. REDIM Array(1 TO 4000) AS FLen  '4000 is a lot of names
  18. DIM Search AS FLen              'best comparing like data
  19.  
  20. FOR X = 1 TO 4000 STEP 2        'impart some realism
  21.   Array(X).LastName = "Henderson"
  22. NEXT
  23.  
  24. Array(4000).LastName = "Henson" 'almost at the end
  25. Search.LastName = "Henson"      'find the same name
  26.  
  27. '----- first time how long it takes using Compare
  28. Start! = TIMER                  'time how long it takes
  29.  
  30. FOR X = 1 TO 5                  'search five times
  31.    FoundAt = SearchType%(Array(), Search)
  32. NEXT
  33.  
  34. IF FoundAt >= 0 THEN
  35.   PRINT "Found at element"; FoundAt
  36. ELSE
  37.   PRINT "Not found"
  38. END IF
  39.  
  40. Done! = TIMER
  41. PRINT USING "##.## seconds with Compare"; Done! - Start!
  42. PRINT
  43.  
  44.  
  45. '----- then time how long it takes using Compare2
  46. Start! = TIMER                  'time how long it takes
  47.  
  48. FOR X = 1 TO 5                  'search five times
  49.    FoundAt = SearchType2%(Array(), Search)
  50. NEXT
  51.  
  52. IF FoundAt >= 0 THEN
  53.   PRINT "Found at element"; FoundAt
  54. ELSE
  55.   PRINT "Not found"
  56. END IF
  57.  
  58. Done! = TIMER
  59. PRINT USING "##.## seconds with Compare2"; Done! - Start!
  60. PRINT
  61.  
  62.  
  63. '---- finally, time how long it takes using pure BASIC
  64. Start! = TIMER
  65.  
  66. FOR X = 1 TO 5
  67.    FoundAt = SearchType3%(Array(), Search)
  68. NEXT
  69.  
  70. IF FoundAt >= 0 THEN
  71.   PRINT "Found at element"; FoundAt
  72. ELSE
  73.   PRINT "Not found"
  74. END IF
  75.  
  76. Done! = TIMER
  77. PRINT USING "##.## seconds using straight BASIC"; Done! - Start!
  78.  
  79. FUNCTION SearchType% (Array() AS FLen, Sought AS FLen) STATIC
  80.  
  81. SearchType% = -1                'assume not found
  82.  
  83. FOR X = LBOUND(Array) TO UBOUND(Array)
  84.   IF Compare%(Array(X), Sought, LEN(Sought)) THEN
  85.     SearchType% = X             'save where it was found
  86.     EXIT FOR                    'and skip what remains
  87.   END IF
  88. NEXT
  89.  
  90. END FUNCTION
  91.  
  92. FUNCTION SearchType2% (Array() AS FLen, Sought AS FLen) STATIC
  93.  
  94. SearchType2% = -1               'assume not found
  95.  
  96. FOR X = LBOUND(Array) TO UBOUND(Array)
  97.   IF Compare2%(Array(X), Sought, LEN(Sought)) THEN
  98.     SearchType2% = X            'save where it was found
  99.     EXIT FOR                    'and skip what remains
  100.   END IF
  101. NEXT
  102.  
  103. END FUNCTION
  104.  
  105. FUNCTION SearchType3% (Array() AS FLen, Searched AS FLen) STATIC
  106.  
  107. SearchType3% = -1               'assume not found
  108.  
  109. FOR X = LBOUND(Array) TO UBOUND(Array)
  110.   IF Array(X).LastName = Searched.LastName THEN
  111.     SearchType3% = X            'save where it was found
  112.     EXIT FOR                    'and skip what remains
  113.   END IF
  114. NEXT
  115.    
  116. END FUNCTION
  117.