How does the "For Each ... Next" statement work so quickly?

L

Larry

Here's a macro I use to get rid of any extra formatting that's been
applied on top of text in the document in addition to the styles, in
order to return the document to its proper style formatting. I use this
macro by itself and also run it from some other macros, and it works
fine. Then I was thinking, since this macro has to stop at each
paragraph in the document, maybe it's slowing things up more than
necessary, and maybe I should use the Search and Replace operation
instead since that works so fast. But then I tested the macro on a
fairly long, 60-page, 160-paragraph document, and the macro was
completed almost instantaneously, in a fraction of a second. So I'm
wondering (very basic question I realize), how does it work so fast?

Another question: is there VBA code that would return the exact time
that a macro takes to run?

Dim myPara As Paragraph
For Each myPara In ActiveDocument.Paragraphs
myPara.Style = myPara.Style
Next myPara

Thanks,
Larry
 
D

DA

The Timer function should do what you want.
It returns a Single representing the number of seconds
elapsed since midnight.

Check your VBA help file for an example.
 
H

Helmut Weber

Hi Larry,
regarding your question concerning speed,
I don't know exactly why and I don't want
to speculate either, though:
ActiveDocument.Range.Font.Reset
should be still faster.
A very simple way to calculate time (seconds)
within a day (doesn't work over midnight without
modification):
Dim lSec As Long
lSec = CLng(Timer)
' do what you like
MsgBox CLng(Timer) - lSec
 
B

Brian

I have had cases with very large numbers of members of a collection where
the macro runs incredibly slowly (hours), specifically with the collection
of spelling errors in a document. I ultimately abandoned the For Each ...
Next approach in that situation.

-Brian
 
J

Jezebel

You'll likely find that the timer is not accurate enough to give you a
reliable comparison between methods, since the timer calls are affected by
the nature of other activities on the computer at the time. A better method
is to use the GetTickCount API call, which is intended for precisely this
sort of use.
 
W

Word Heretic

G'day "Brian" <bpollak@_no_spam_hotmail.com>,

a 160 paragraph document is tiny lol


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Brian reckoned:
 
K

Klaus Linke

Hi Brian,

Next time, try to comment out the code inside the "For Each", and test
again.

I bet you'll find that the "For Each" itself is really fast, but the things
that are done for each member in the collection are slow.

In the case of spelling errors, perhaps the code forced Word to spell-check
the document anew in each iteration?

Regards,
Klaus
 
L

Larry

Thanks for the replies from everyone. I'll try the timer advice.

But I'm still asking this basic question; how does vba do the stuff
that it's doing so incredibly fast? It seems to me we're talking
thousandths of a second for various steps.

Larry
 
C

Cindy M -WordMVP-

Hi Larry,
But I'm still asking this basic question; how does vba do the stuff
that it's doing so incredibly fast? It seems to me we're talking
thousandths of a second for various steps.
Because computers are fast, and keep getting faster :)

Your specific question is why For Each Next is faster than
Find/Replace?

The object model stores things in collections (Paragraphs is a
collection). Each collection has a set of properties
(Paragraph.Style). When you do a For Each Next, VBA basically runs
down a table column (the "table" would be the set of all paragraphs;
the column the Style entry for each paragraph) and compares the
values. If the value doesn't match, it changes that.

Find/Replace has to process the entire document text, character by
character, more or less (depending on the algorithm).

Looked at it like this, it would be much faster for you, as well, to
use the "For Each Next" technique to find and change something, than
to run through 160 pages of text :)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
L

Larry

Thanks, Cindy, that's interesting. What you're saying is, For Each Next
is fast because it's not working its way through the document text, it's
working directly on the objects which exist apart from the document
text.

Larry
 
W

Word Heretic

G'day "Larry" <[email protected]>,

Its a compiled routine in C, that's how fast compiled programs run
compared to interpreted programs (VBA).

For VBA code to execute, a little software counter has to move to the
next token in the range, look up its offset in the function table,
extract the parameters as pointers to variables, constants or objects,
decide which of these it actually is, perform some safety steps like
type checking and pass them to the routine.

THEN something happens, and boy is it quick, coz its just "Yo
Computer! Do this, this this this this this this AND QUIT!" slam, bam
outa there.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Larry reckoned:
 
C

Cindy M -WordMVP-

Hi Larry,
What you're saying is, For Each Next
is fast because it's not working its way through the document text, it's
working directly on the objects which exist apart from the document
text.
That's basically correct, yes :)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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