+ Reply to Thread
Results 1 to 19 of 19

Make code faster/better best practices

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    This is what i came up with .....

    hi Shg, below is the code i came up with based on your helpful inputs. I learned alot.

    Sub compare_lists(droparray() As String, addarray() As String, Currentlist As Range, Newlist As Range)
    
    Dim x As Long
    Dim mtch As Boolean
    Dim array_cntr As Long
    Dim cntr As Long
    
    
    'droparray
    array_cntr = 1
    For x = 1 To Currentlist.Count
        cntr = 1
        mtch = False
        Do
            If Currentlist.Cells(x, 1) = Newlist.Cells(cntr, 1) Then
                mtch = True
            Else: cntr = cntr + 1
            End If
            If mtch Or (cntr > Newlist.Count) Then Exit Do
        Loop
        If Not mtch Then
        ReDim Preserve droparray(array_cntr)
        droparray(array_cntr) = Currentlist.Cells(x, 1)
        array_cntr = array_cntr + 1
        End If
    Next x
    
    'addarray
    array_cntr = 1
    For x = 1 To Newlist.Count
        cntr = 1
        mtch = False
        Do
            If Newlist.Cells(x, 1) = Currentlist.Cells(cntr, 1) Then
                mtch = True
            Else: cntr = cntr + 1
            End If
            If mtch Or (cntr > Currentlist.Count) Then Exit Do
        Loop
        If Not mtch Then
        ReDim Preserve addarray(array_cntr)
        addarray(array_cntr) = Newlist.Cells(x, 1)
        array_cntr = array_cntr + 1
        End If
    Next x
    
    'for debug
    'For x = 1 To UBound(droparray())
    'Workbooks("working_file.xlsm").Worksheets("compare_test").Range("j" & x + 1) = droparray(x)
    'Next x
    
    'for debug
    'For x = 1 To UBound(addarray())
    'Workbooks("working_file.xlsm").Worksheets("compare_test").Range("k" & x + 1) = addarray(x)
    'Next x
    
    
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You should compare performance of the two approaches with perhaps 25,000 names.

    Redim Preserve is an expensive operation; that's why I resized the arrays to their max possible size at the outset.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    Hi Shg,

    Sure i can do that. How do i calculate the time for a procedure to run? Do you know an easy way?

    David

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Application.Timer returns the number of seconds since midnight, including fractional seconds on Windows (whole seconds only on Mac).

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    Hi Shg,

    I ran two tests. One using your program and one using mine. I used the same names that were in your original spread sheet. I think the first list had ~1688 entries and the 2nd has ~ 1679 entries.

    Yours took ~ 0.281 sec
    Mine took ~ 39sec

    Thats a BIGGGGGGGGGGGGG difference.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Mark as solved?

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    Shg,

    Yours is definitely faster. Thanks.

    Yes i will mark as solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1