Re: Efficiency question
From: Greg (anonymous_at_discussions.microsoft.com)
Date: 08/19/04
- Next message: Peter: "Re: If Word already open, creating new doc via code fouls things up"
- Previous message: Simon Ransom: "Changing Font Color in table...depending on value"
- In reply to: Peter Hewett: "Re: Efficiency question"
- Next in thread: Peter Hewett: "Re: Efficiency question"
- Reply: Peter Hewett: "Re: Efficiency question"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 19 Aug 2004 10:44:55 -0700
Peter,
Thanks for the explaination. I am not really doing
anything for efficiency or any practical purpose for that
matter. I just experimenting with a macro for the
learning experience. It has become quite a hairball as it
grows.
>-----Original Message-----
>Hi Greg
>
>Without trying to overly complicate the issue I'll make
the following statement: "You
>don't always just code for efficiency". Sometimes it's
more appropriate to write for
>clarity. I prefer the first version as it does not
duplicate code as the second example
>does. Also I hate the use of the colon (line continuation
character). The only time you
>should use this is to make your code more readable and in
this case it achieves the
>opposite.
>
>Unless the loops running thousands of time I wouldn't
worry too much about efficiency! If
>you're looking for efficiency, you may do better with
something like:
>
> Dim hciDefault As Word.WdColorIndex
> Dim boolHighlight As Boolean
>
> ' Set this outside the loop as it saves an object
repeated reference
> hciDefault = Options.DefaultHighlightColorIndex
>
> ' Do this outside the loop as it saves a repeated
comparison
> boolHighlight = pHlight = vbYes
>
> ' I've removed the True comparison but the compiler
may remove it anyway
> Do While .Execute
> If boolHighlight Then
>
> ' Use with so save a repeated object reference
> With rngStory
> .HighlightColorIndex = hciDefault
> .Collapse wdCollapseEnd
> End With
> End If
> pCount = pCount + 1
> Loop
>
>to be quite honest, given the overhead of having Word
searching a document the
>optimisation is truly redundant.
>
>HTH + Cheers - Peter
>
>
>"Greg" <anonymous@discussions.microsoft.com>, said:
>
>>I am reviewing my code looking for efficiencies.
>>
>>The code looks for text, counts occurrences, and "if"
the
>>user elects, it will highlight each occurrence.
>>
>>Here is a bit of code that is working, but seems
>>inefficient:
>>
>>Do While .Execute = True
>> If pHlight = vbYes Then
>> rngStory.HighlightColorIndex _ =
>> Options.DefaultHighlightColorIndex
>> rngStory.Collapse wdCollapseEnd
>> End If
>> pCount = pCount + 1
>>Loop
>>
>>Since pHight is set before the Do ... Loop starts, and
>>won't change during the Loop am I not wasting time
>>checking it each run through the Loop?
>>
>>Would it be more efficient to have two Do ... Loops in
an
>>IF Else statement like this:
>>
>>If pHlight = vbYes Then
>> Do While .Execute = True
>> rngStory.HighlightColorIndex _ =
>> Options.DefaultHighlightColorIndex
>> rngStory.Collapse wdCollapseEnd
>> pCount = pCount + 1
>> Loop
>>Else: Do While .Execute = True
>> pCount = pCount + 1
>> Loop
>>End If
>>
>>I think I am asking a stupid question with an obvious
>>answer. Just looking for confrimation that I am on the
>>right track here or a recommendation for a better way.
>>
>>
>
>.
>
- Next message: Peter: "Re: If Word already open, creating new doc via code fouls things up"
- Previous message: Simon Ransom: "Changing Font Color in table...depending on value"
- In reply to: Peter Hewett: "Re: Efficiency question"
- Next in thread: Peter Hewett: "Re: Efficiency question"
- Reply: Peter Hewett: "Re: Efficiency question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|