简体   繁体   中英

Array search/compare is slow, compare to Excel VBA

I just switched from VBA (Excel) to VB (Visual Studio Express 2013). Now I have copied parts of my code from VBA to VB. And now I'm wondering why VB is so slow... I'm creating an Array (IFS_BV_Assy) with 4 column and about 4000 rows. There are some identical entrys in it, so I compare every entry with each other and override the duplicate with a empty string. The Code looks like that:

    For i = 1 To counter
        For y = 1 To counter
            If IFS_BV_Assy(1, y) = IFS_BV_Assy(1, i) And i <> y Then
                If IFS_BV_Assy(2, i) < IFS_BV_Assy(2, y) Then
                    IFS_BV_Assy(1, i) = ""
                Else
                    IFS_BV_Assy(1, y) = ""
                End If
                Exit For
            End If
        Next
    Next

Counter is the lenght of the Array.

In VBA it takes about 1 Sec. In VB it takes about 30 Sec. to go thru the loop. Somebody knows why? (im creating some Timestamp between every Step to be sure whats slow. And that loop is the bad guy)

The Array looks like this:

  • (1,1) = 12.3.015 / (2,1) = 02
  • (1,2) = 12.3.016 / (2,2) = 01 <-- delete
  • (1,3) = 12.3.016 / (2,3) = 02 <-- keep, because 02 is newer then 01
  • (1,4) = 12.3.017 / (2,4) = 01
  • (1,5) = 12.3.018 / (2,5) = 01

Thanks in advance

Andy

Edit: I create the Array like that:

    strStartPath_BV_Assy = "\\xxx\xx\xx\"
    myFile = Dir(strStartPath_BV_Assy & "*.*")
    counter = 1
    ReDim IFS_BV_Assy(0 To 2, 0 To 0)
    IFS_BV_Assy(0, 0) = "Pfad"
    IFS_BV_Assy(1, 0) = "Zg."
    IFS_BV_Assy(2, 0) = "Rev"

    Do While myFile <> ""
        If UCase(Right(myFile, 3)) = "DWG" Or UCase(Right(myFile, 3)) = "PDF" Then
            ReDim Preserve IFS_BV_Assy(0 To 2, 0 To counter)
            IFS_BV_Assy(0, counter) = strStartPath_BV_Assy + myFile
            IFS_BV_Assy(1, counter) = Left(Mid(myFile, 12), InStr(1, Mid(myFile, 12), "-") - 1)
            IFS_BV_Assy(2, counter) = Mid(myFile, Len(myFile) - 8, 2)
            counter = counter + 1
        End If
        myFile = Dir()
    Loop

Maybe data was best case (around 4000) when ran in VBA. 30 sec seems a reasonable time for 4000x4000=16.000.000 iterations. 1 sec is too low for this number of iterations.

Stokke suggested to create the array as String instead of Objekt Type.

Dim IFS_BV_Assy(,) as String

I create the Module with Option Explicit Off , because I never see any difference in VBA for that point. Now I declare any variable with Dim .. as ... . And now, it's as fast as VBA is =)

Learning = making mistakes.. =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM