I got a theoretical question, will appreciate if you advise me here.
Say, we have these two pieces of code.
First one:
For Each cell In rng1
collectionOfValues.Add (cell.Value)
Next
For Each cell In rng2
collectionOfAddresses.Add (cell.Address)
Next
For i = 1 To collectionOfAddresses.Count
Range(collectionOfAddresses.Item(i)) = collectionOfValues.Item(i)
Next i
Here we add addresses from one range to a certain collection, and values from another range to a second collection, and then fill cells on these addresses with the values.
Here is the second code, which makes the same:
For i = 1 To rng1.Rows.Count
For j = 1 To rng1.Columns.Count
rng2.Cells(i, j) = rng1.Cells(i, j)
Next j
Next i
So, the question is - what is the time of execution in both cases? I mean, it's clear that the second case is O(n^2) (to make it easier we assume the range is square).
What about the first one? Is For Each considered a nested loop?
And if so, does it mean that the time of the first code is O(n^2) + O(n^2) + O(n^2) = 3*O(n^2) which makes pretty the same as the second code time?
In general, do these two codes differ apart from the fact that the first one takes additional memory when creating collections?
Thanks a lot in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…