Jay Freedman wrote:
[..]
I don't know for sure. It depends on how MS implemented the .Index
property. If the runtime has to count from the top of the table each
time to get that number, it could get very slow for very large tables.
The comparison is trivial compared to that.
Hmm, this is no array with fixed dimensions where one would presume the
same amount of time to fetch a[100] compared to a[1], right.
For the same table, though, I think the second example would be even
worse because the statement
Set oRow = oTbl.Rows(rowNum)
does involve counting from the top of the table, plus an object
assignment.
Feel free to run some tests and report the results.
I'd rather bow to your judgement.
Learnt something from that today already!
Greetinx
Robert
OK, Mr. Idle Curiosity, here are some test data for tables of several
sizes:
nRows T1(sec) T2(sec)
500 9.5 12.1
800 24.1 30.9
1200 58.7 75.0
1500 93.4 118.6
Plotting these in Excel and applying trendlines, it's clear that both
series are quadratic (that is, the time is a function of the square of
the number of rows). The first example is about 30% faster than the
second one, but they both chew up a lot of processor time.
In fact, on this system (Athlon 2800 with 2 GB RAM) both macros cause
the CPU utilization to jump up to 100% and stay there the whole time.
That causes the CPU temperature to go from 55C to 60C, which makes me
rather unhappy... I don't want to try your 10 000 rows for fear of
frying the system.
The code used for the tests was this, in which the "processing" of
each row was just to make the text bold in the first cell:
Sub test()
Const nRows = 1500
Dim sampleDoc As Document
Dim startTime As Single, stopTime As Single
Dim sample1Time As Single, sample2Time As Single
Dim msg As String
Set sampleDoc = MakeTable(nRows)
startTime = Timer
Call sample1(sampleDoc)
stopTime = Timer
sample1Time = stopTime - startTime
sampleDoc.Close savechanges:=wdDoNotSaveChanges
Set sampleDoc = Nothing
MsgBox "Finished part 1"
Set sampleDoc = MakeTable(nRows)
startTime = Timer
Call sample2(sampleDoc)
stopTime = Timer
sample2Time = stopTime - startTime
sampleDoc.Close savechanges:=wdDoNotSaveChanges
Set sampleDoc = Nothing
msg = "For Each took " & sample1Time & " sec" & vbCr
msg = msg & "For rowNum took " & sample2Time & " sec"
MsgBox msg
End Sub
Sub sample1(oDoc As Document)
Dim oTbl As Table
Dim oRow As Row
Set oTbl = oDoc.Tables(1)
For Each oRow In oTbl.Rows
' don't process if it's row 1
If oRow.Index > 1 Then
oRow.Cells(1).Range.Bold = True
End If
Next
End Sub
Sub sample2(oDoc As Document)
Dim oTbl As Table
Dim oRow As Row
Dim rowNum As Long
Set oTbl = oDoc.Tables(1)
' start processing at row 2
For rowNum = 2 To oTbl.Rows.Count
Set oRow = oTbl.Rows(rowNum)
oRow.Cells(1).Range.Bold = True
Next
End Sub
Function MakeTable(nRows As Long) As Document
Dim oDoc As Document
Dim idx As Long
Dim txt As String
Dim r As Range
Const s = "Hello" & vbTab
Set oDoc = Documents.Add
For idx = 1 To nRows
txt = txt & s & CStr(idx) & vbCr
Next
Set r = oDoc.Range
With r
.Collapse wdCollapseStart
.Text = txt
.MoveEnd wdCharacter, -1
.ConvertToTable
End With
Set MakeTable = oDoc
End Function
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.