home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / pascal / 5325 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  2.2 KB

  1. Xref: sparky comp.lang.pascal:5325 alt.msdos.programmer:2351
  2. Newsgroups: comp.lang.pascal,alt.msdos.programmer
  3. Path: sparky!uunet!usc!sol.ctr.columbia.edu!ira.uka.de!fauern!dec16!dec8!schoof
  4. From: schoof@dec8.informatik.uni-wuerzburg.dbp.de (Jochen "Joscho" Schoof)
  5. Subject: Re: 3 questions
  6. Message-ID: <1992Sep11.105333.10698@informatik.uni-wuerzburg.de>
  7. Sender: news@informatik.uni-wuerzburg.de (USENET News account)
  8. Organization: University of Wuerzburg, Germany
  9. References: <18ot9rINN67t@matt.ksu.ksu.edu>
  10. Date: Fri, 11 Sep 1992 10:53:33 GMT
  11. Lines: 46
  12.  
  13. From article <18ot9rINN67t@matt.ksu.ksu.edu>, by holland@matt.ksu.ksu.edu (Rich Holland):
  14. [some lines deleted]
  15. > Question 2:
  16. > -----------
  17. >   In general, what's the most efficient (fastest) way to search for a small
  18. >   string inside a larger one (or for a group of say 5 bytes in a group
  19. >   of say 1000 bytes)?
  20. >   
  21. >   I'd thought of something like this:
  22. >   For i:=1 to Length(String2)-Length(String1) do       
  23. >     Begin
  24. >     If String1=Copy(String2,i,Length(String1)) then {we found it}
  25. >                                                     { so quit for-loop }
  26. >     Else { keep looking }
  27. >     End;
  28. [some lines deleted]
  29.  
  30. I think the better way is to check only occurancies of the substring's
  31. first letter like in:
  32.  
  33.    FOR i:=1 TO Length(String2)-Length(String1) DO BEGIN
  34.      IF String2[i]=String1[1] THEN
  35.        IF String1=Copy(String2,i,Length(String1)) THEN Found:=TRUE;
  36.  
  37. I think you are using Turbo Pascal and therefore you should avoid the
  38. Copy-function where possible, because it is very slooooooooow. A good
  39. hint when looking for algorithms like this is the following book:
  40.  
  41.   Robert Sedgewick
  42.   Algorithms
  43.   Addison-Wesley
  44.  
  45. It offers you lots of standard-algorithms in Pascal source. Special
  46. version for C and C++ are also available. The above algorithm is not
  47. from that book, but the first idea I got, so I'm sure it's not most
  48. effective.
  49. Hope I helped you
  50.  
  51. - Jochen
  52.  
  53. -- 
  54. ---------------------------------------------------------------------------
  55. Jochen Schoof                    schoof@sundae.informatik.uni-wuerzburg.de
  56. Lehrstuhl f. Informatik II +-----------------------------------------------
  57. Universitaet Wuerzburg     | It's worse than that: he's dead, Jim!  -Bones
  58.