home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.tcl
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!menudo.uh.edu!sugar!karl
- From: karl@NeoSoft.com (Karl Lehenbauer)
- Subject: Re: Inefficient string handling
- Organization: NeoSoft Communications Services -- (713) 684-5900
- Date: Fri, 11 Sep 1992 21:30:21 GMT
- Message-ID: <1992Sep11.213021.24877@NeoSoft.com>
- References: <1992Sep10.205122.25788@walter.bellcore.com>
- Lines: 45
-
- In article <1992Sep10.205122.25788@walter.bellcore.com> Nathaniel Borenstein
- <nsb@thumper.bellcore.com> writes that wraplines, below, is slow.
-
- >proc wraplines {txt} {
- > set ans ""
- > set index 0
- > set column 0
- > set len [string length $txt]
- > for {} {$index < $len} {incr index} {
- > set thischar [string index $txt $index]
- > append ans $thischar
- > incr column
- > if {$thischar == "\n"} {set column 0}
- > if {$thischar == " " && $column > 70} {set column 80}
- > if {$column > 79} {
- > append ans "\n"
- > set column 0
- > }
- > }
- > return $ans
- >}
-
- Tcl can get really slow if you do a lot of stuff character-at-a-time like
- this.
-
- As for improvements, note that no matter what, your program does nothing to
- the first seventy characters of each line except copy them across.
-
- So this suggests an approach where you start your index at 70 and walk it
- 'til you find you blank (or not), then do an
- append ans "[string range $lo $hi]\n"
- when you know exactly what you want.
-
- That alone should speed your routine by more than two orders of magnitude.
-
- If you can cook up a regular expression that does what you want, "regsub"
- might be worth looking into.
-
- Oh, and you could use Extended Tcl's "loop" command, instead of "for", to
- eke out a bit more performance still, as loop will avoid at least one "eval"
- and one "expr" call per time through the loop.
- --
- -- Email info@NeoSoft.com for info on getting interactive Internet access.
- You will now awaken feeling relaxed and refreshed, remembering everything
- you've read except the details of the Omega contingency plan.
-