Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Good Code - Bad Code (Word97SR2)

    This maybe a good long thread, dragging on for years - maybe not.

    I was astounded at the time it took to excute the .Count (2nd example) on my 200,000 character document. Figured there had to be a better way, and there was (1st example).

    <pre> If doc.Characters.Last = Chr$(13) Then
    doc.Characters.Last.Delete
    Else
    End If
    </pre>


    <pre> If doc.Characters(doc.Characters.Count) = Chr$(13) Then
    doc.Characters(doc.Characters.Count).Delete
    Else
    End If
    </pre>


  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Word is full of funny little properties like that.

    But from your subject line, I thought it was a logic problem:

    Good Code [minus] Bad Code [equals] ??

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    > [equals] ??

    I dunno. If there's bad code, I probably figure into the equation somewhere (grin!).

    I know that my early code is full of references to documents by their full file names, instead of as Document objects. I've gotten rid of most of my Selections, but who knows just how slow some of my early code is running, because I was so relieved to find a way of doing it, and didn't know to continue inspecting the rest of an Object's Properties.

  4. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Chris,

    I assume Word always knows the last character,and so can just go there and delete it.

    However the character count is dynamic and so has to be counted, which would cause your second example to be somewhat slower than the first.

    However if you run the second example for a second time then it should run almost as fast as the first example provided that the document has not been changed since the first run. Word seems to only do a count when asked and will remember that property until something in the document changes.A fresh count would the be performed when the property is invoked.

    Andrew

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Correct, correct, and correct. Three out of three. Thank you!

    I tried your suggestion and while the .Count takes nearly 30 seconds on my 733MHz (the Word document is 6.5MB of plain ascii text characters), the .Last is instantaneous. These two tests were run on a freshly-loaded copy of Word97SR2 + the document.

    A second run of the .Count is instantaneous.
    Inserting one (space) character at the front of the document and .Count reverts to 30 seconds.
    Inserting one (space) character at the end of the document and .Count reverts to 30 seconds.

  6. #6
    Star Lounger
    Join Date
    Jan 2001
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    As far as I see it there are a few

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Chris

    I've taken an interest in such efficiency/ inefficiency matters myself recently. I found a good site discussing some of these issues for Excel VBA. I've concluded that generally, the use of a "built-in" property or native procedure is always going to be more efficient than anything you could dream up yourself. The hard bit is knowing the properties and functions to cash in on. I'd like to find a treatment similar to this one for other Office programs.

    Another factor here is the unavoidable overheads of interpretted code and running bytecode or p-code or whatever it's called in VB, compared with "real" compiled code. I like efficiency - I only wish I was good at it <img src=/S/grin.gif border=0 alt=grin width=15 height=15>.

    Alan

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    > Five: The last character in a document is always Chr$(13), and you can

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Good Code - Bad Code (Word97SR2)

    > Three: Never count on the Count property in Word

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    > you

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    > I like efficiency - I only wish I was good at it

    [soapbox] Define "efficiency" [/soapbox]

    Years ago I took it always to mean execution speed, but had that drummed out of me after spending two weeks devising a speedup of milliseconds in a payroll program. My supervisor multiplied my savings by the nationwwide number of employyees of BHP, multiplied THAT by 26 weeks, and told me I'd save about two minutes on the IBM 1401 over an entire year, and to stop wasting my time and his!


    Now I think of efficiency in several forms, usually in this order:
    1) how quickly can I get a draft or prototype out the door to get the client to sign off on the contract
    2) how quickly can I write the real thing and get paid
    3) how quickly can I effect modifications
    4) how quickly does it run.

    Execution speed is usually my lowest priority, because anything I write is vastly faster or less error-prone that the current manual method.


    I thnk that if one is writing readable code that is error-free, one is efficient. Such a view includes another question somewhere "How can I claim to efficient if I am not making use of a library of utility procedures", and much much more.

    In the example I posted, it was the HUGE delay (30 seconds I think), that jolted me to examine execution speed, otherwise I wouldn't have given it a second thought.

  12. #12
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Agree - there are lots of factors to factor in. Your comments on "readable code" and "effect modifications" have become more significant to me recently, having to pass a lot of my stuff onto others for possible further "tailoring". In this context, I've had to sacrifice what I'd think of as "efficiency" to make things more readable and comprehendable to other parties. Like life itself, it's all one big compromise. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Alan

  13. #13
    New Lounger
    Join Date
    May 2001
    Location
    Blackpool, Lancashire, England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Just been browsing, and finding I'm now doing a shed load of development in word VBA, programatically building and formatting documents, I couldnt resist begging an elaboration on the "I've gotten rid of most of my Selections" statement. I am using a lot of selections to populate the document from a class of paragraph objects, find specific text and apply style properties. Anything that helps this big template with efficiency would be gratefully recieved. Chrs JasH

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Selection (keyboard or mouse) is the mechanism provided by Word for the End-User to identify data (text) on which some action is to be performed.
    Ranges (via program code) is the mechanism provided by Word for the Developer to identify data (text) on which some action is to be performed.

    Selection (in Word) is limited to one contiguous block of data at a time; The developer can define/use several Ranges simultaneously.

    As a programmer, I can work with several ranges of data at once, and that is an advantage in logic.

    It has seemed to me (but I've not timed it rigorously) that even with ScreenUpdating turned OFF, code that uses Selection runs significantly slower than code using Ranges.

    Ranges have other advantages - the ability to "stretch" the range my incrementing the .End value, and so on.

    I'm finding it hard to express my thoughts this morning, but I feel that once i dropped Selection (except for the end-user identifying text for me) my power and code improved considerably.

  15. #15
    New Lounger
    Join Date
    May 2001
    Location
    Blackpool, Lancashire, England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Good Code - Bad Code (Word97SR2)

    Thx for that have 10 modules containing a dozen plus mods each building a different document:
    With ObjDoc
    .Bookmarks.Add "YPara58" 'inserts bookmark
    .Bookmarks("YPara58").Select
    Selection.TypeText Text:=objYParas.YPara58.Text
    Selection.Style = objYParas.YPara58.Style
    If objYParas.YPara58.StyleModifiersFlag Then
    With Selection.Find
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = True
    .Wrap = wdFindContinue
    .Execute FindText:=objYParas.YPara58.StyleModifiersText
    End With
    Selection.Style = objYParas.YPara58.StyleModifiersStyle
    End If
    Selection.WholeStory
    Selection.Collapse wdCollapseEnd
    Selection.TypeText Text:=Chr(13)
    I think I'll get the stopwatch out and have a dabble and check out apparent build efficiencies. Chrs JasH

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •