Building collection loop efficiency (1 Viewer)

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:35
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
 

June7

AWF VIP
Local time
Today, 13:35
Joined
Mar 9, 2014
Messages
5,423
Time the procedures and find out which is faster.

dteStart = Now()
'run procecure
Debug.Print DateDiff("s", dteStart, Now())
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:35
Joined
Oct 29, 2018
Messages
21,358
Also, wouldn’t it be simpler to use a Select Case statement?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:35
Joined
Feb 28, 2001
Messages
27,001
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.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:35
Joined
May 21, 2018
Messages
8,463
No difference.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:35
Joined
Feb 28, 2001
Messages
27,001
MajP says NO DIFFERENCE, I say MARGINAL DIFFERENCE AT BEST. So in the end analysis, you make your choice and take your chances.
 

snow-raven

Registered User.
Local time
Today, 14:35
Joined
Apr 12, 2018
Messages
48
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!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:35
Joined
May 21, 2018
Messages
8,463
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.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:35
Joined
Feb 28, 2001
Messages
27,001
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."
 

isladogs

MVP / VIP
Local time
Today, 21:35
Joined
Jan 14, 2017
Messages
18,186
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

Top Bottom