home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / tcl / 1344 < prev    next >
Encoding:
Text File  |  1992-09-11  |  2.0 KB  |  56 lines

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!menudo.uh.edu!sugar!karl
  3. From: karl@NeoSoft.com (Karl Lehenbauer)
  4. Subject: Re: Inefficient string handling
  5. Organization: NeoSoft Communications Services -- (713) 684-5900
  6. Date: Fri, 11 Sep 1992 21:30:21 GMT
  7. Message-ID: <1992Sep11.213021.24877@NeoSoft.com>
  8. References: <1992Sep10.205122.25788@walter.bellcore.com>
  9. Lines: 45
  10.  
  11. In article <1992Sep10.205122.25788@walter.bellcore.com> Nathaniel Borenstein 
  12. <nsb@thumper.bellcore.com> writes that wraplines, below, is slow.
  13.  
  14. >proc wraplines {txt} {
  15. >    set ans ""
  16. >    set index 0
  17. >    set column 0
  18. >    set len [string length $txt]
  19. >    for {} {$index < $len} {incr index} {
  20. >        set thischar [string index $txt $index] 
  21. >        append ans $thischar
  22. >        incr column
  23. >        if {$thischar  == "\n"}  {set column 0}
  24. >        if {$thischar  == " " && $column > 70}  {set column 80}
  25. >        if {$column > 79} {
  26. >            append ans "\n"
  27. >            set column 0
  28. >        }
  29. >    }
  30. >    return $ans
  31. >}
  32.  
  33. Tcl can get really slow if you do a lot of stuff character-at-a-time like
  34. this.
  35.  
  36. As for improvements, note that no matter what, your program does nothing to
  37. the first seventy characters of each line except copy them across.
  38.  
  39. So this suggests an approach where you start your index at 70 and walk it
  40. 'til you find you blank (or not), then do an
  41.     append ans "[string range $lo $hi]\n"
  42. when you know exactly what you want.
  43.  
  44. That alone should speed your routine by more than two orders of magnitude.
  45.  
  46. If you can cook up a regular expression that does what you want, "regsub"
  47. might be worth looking into.
  48.  
  49. Oh, and you could use Extended Tcl's "loop" command, instead of "for", to
  50. eke out a bit more performance still, as loop will avoid at least one "eval"
  51. and one "expr" call per time through the loop.
  52. -- 
  53. -- Email info@NeoSoft.com for info on getting interactive Internet access.
  54. You will now awaken feeling relaxed and refreshed, remembering everything 
  55. you've read except the details of the Omega contingency plan.
  56.