Efficiency question

G

Greg

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.
 
P

Peter Hewett

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
 
G

Greg

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
 
J

Jonathan West

Hi Greg,

It depends on what you are being efficient about. In most cases, shaving a
few percent of the runtime of a process doesn't affect the overall apparent
speed by any sigificant amount, so you want to to be efficient in the time
it takes to code things.

It is only in a relatively small number of cases - mainly loops - where
execution time really matters.

The way I approach things is not to worry all that much about speed of
execution when I first write some code, but instead concentrate on keeping
the code short and maintainable by having as few duplications as possible.

When I have run it & debugged it, I then look to see if anything is
performing overly sluggishly and take a look to see what can be done to
speed it up.

In the particular case of the loop that you have mentioned, the time spend
executing "If pHlight = vbYes Then" is very small compared to the time spent
on any line of code that makes an access to the object model. This means
that although the second version will theoretically run faster, the
difference in speed is unlikely to be noticeable.
 
P

Peter Hewett

Hi Greg

Don't preen your code too much or you may get a fur-ball stuck in your throat!

Cheers - Peter


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.

duplicate code as the second example
worry too much about efficiency! If

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top