Building collection loop efficiency

Which loop method is more efficient?

  • name appropriate tags, add to collection

    Votes: 1 100.0%
  • name tag, add to appropriate collections

    Votes: 0 0.0%

  • Total voters
    1
  • Poll closed .

snow-raven

Registered User.
Local time
Today, 14:06
Joined
Apr 12, 2018
Messages
48
General looping efficiency question for building a collection of controls;

Do you think I'd be more efficient evaluating all of the property tags that I want to add to a collection before moving to the next collection, or would it be more efficient evaluating each tag & adding it to all of the appropriate collections before moving to the next tag?

Example:

Code:
For Each ctl In Me.Controls
    If ctl.Tag = "Project" Or ctl.Tag = "Intervals" Or ctl.Tag = "Lab" Or ctl.Tag = "TestSetUp" (etc...) Then
        colctlsAll.Add ctl, ctl.Name
    End If
    If ctl.Tag = "Collar" Or ctl.Tag = "Intervals" Or ctl.Tag = "Lab" Or ctl.Tag = "TestSetUp" (etc...) Then
        colctlsNewPrjt.Add ctl, ctl.Name
    End If
    If ctl.Tag = "Intervals" Or ctl.Tag = "Lab" Or ctl.Tag = "TestSetUp" (etc...) Then
        colctlsNoCllr.Add ctl, ctl.Name
    End If
Next ctl
Set ctl = Nothing

Or:
Code:
For Each ctl In Me.Controls
    If ctl.Tag = "Project" Then
        colctlsAll.Add ctl, ctl.Name
    End If
    If ctl.Tag = "Collar" Then
        colctlsAll.Add ctl, ctl.Name
        colctlsNewPrjt.Add ctl, ctl.Name
    End If
    If ctl.Tag = "Intervals" Then
        colctlsAll.Add ctl, ctl.Name
        colctlsNewPrjt.Add ctl, ctl.Name
        colctlsNoCllr.Add ctl, ctl.Name
    End If
Next ctl
Set ctl = Nothing
 
Time the procedures and find out which is faster.

dteStart = Now()
'run procecure
Debug.Print DateDiff("s", dteStart, Now())
 
Also, wouldn’t it be simpler to use a Select Case statement?
 
I'm of the opinion that you should make the changes to one collection at a time, though it MIGHT be a marginal speed difference.

I say that because of the "collection probes" required. In essence, each collection can be thought of as an array or list. A collection that contains collections is a list that contains lists. It is a nested, double-barrelled search.

If you do the individual INNER additions one at a time, you are repeatedly searching the OUTER collection for a match, then searching the INNER collection. If you instead do the OUTER collection search and, when you find a match, do all INNER items at once (while keeping track of the current collection object, i.e. NOT searching for it - 'cause you found it), I think you do less searching, all other steps being equal.

One man's opinion.
 
No difference.
 
MajP says NO DIFFERENCE, I say MARGINAL DIFFERENCE AT BEST. So in the end analysis, you make your choice and take your chances.
 
Thanks, all. I don't know why I didn't think of applying a timer. I'll try that when I get a second. (hyuk, hyuk)

Doc_Man, I did nearly implement a select case, but I couldn't think of a way to apply it that didn't include it in each If statement, so it didn't seem that it would add much to efficiency. Admittedly, I have even less experience using select case than with loops in general.

Thanks for the feedback, everyone!
 
IMO waste of time. So lets say technique A is 75 milliseconds faster 65% of the time? So what. I severely doubt you can measure this with a timer, you will need something far more advanced and any measure will be noise.
 
Like I said, MajP, minimal difference. What was it that P.T. Barnum's barkers used to say in the side-show carnival? "You pays your money and you takes your chances."
 
Sounds like a job for Col (Isladogs)...

Sorry but I'm going to opt out of this one :rolleyes:
I also see little difference being likely.

Whilst you can easily measure to centisecond (0.01s) accuracy, doing anything more precise than that needs more complex code.
 

Users who are viewing this thread

Back
Top Bottom