比较两个字符串数组并返回vb6中每个字符串的差异,具有良好的性能

时间:2012-02-14 15:22:45

标签: arrays performance vb6 comparison

我有2个带有字符串的可扩展数组,我想比较两者,看看哪些包含另一个不包含的字符串。

假设我的数组如下:

ARRAY - 1       ARRAY - 2
   a1               a1
   a2               a2
   a3               a3
   a9               a4
   a10              a8
   a11              a10
   a12              a11

我想得到的结果是:

ARRAY - 4       ARRAY - 5       ARRAY - 6
   a9               a4             a1
   a12              a8             a2
                                   a3
                                   a10
                                   a11

另外3个不同的数组应该与array2相比给出array1的区别

-array4 here gives the strings that is included in array1 but not found in array2
-array5 here gives the strings that is included in array2 but not found in array1
-array6 here gives the strings that is found in both

为此编码:

i = 0
j = 0

For Each innerElement1 In CompareElement1 'CompareElement1 is the array1 here

    NoneFound = 1

    'Ones thats in first element also found in second element..
    For Each innerElement2 In CompareElement2 'CompareElement2 is the array2 here

        If innerElement1 = innerElement2 Then

            'Expand array
            ReDim Preserve IncludedInBoth(0 To UBound(IncludedInBoth) + 1)
            IncludedInBoth(i) = innerElement1
            'Item found in both so NoneFound is 0.
            NoneFound = 0
            i = i + 1

        End If

    Next

    'Ones thats in first element but not found in second element..
    If NoneFound = 1 Then

        'Expand array
        ReDim Preserve NotIncludedInElem2(0 To UBound(NotIncludedInElem2) + 1)
        NotIncludedInElem2(j) = innerElement1
        j = j + 1

    End If

Next

'Seperate Comparison for the ones that found in second element _
 but not found in first element..
i = 0

For Each innerElement1 In CompareElement2

    NoneFound = 1

    'Ones thats in second element also found in first element.
    For Each innerElement2 In IncludedInBoth

        If innerElement1 = innerElement2 Then

            'Item found in both so NoneFound is 0.
            NoneFound = 0

        End If

    Next

    'Ones thats in second element but not found in first element..
    If NoneFound = 1 Then

        'Expand array
        ReDim Preserve NotIncludedInElem1(0 To UBound(NotIncludedInElem1) + 1)
        NotIncludedInElem1(i) = innerElement1
        i = i + 1

    End If

Next

我的代码在那里确实进行了比较并给出了真正的答案,但由于内部for each调用导致性能不足,有没有办法比较快速的方式?它就像永远完成这个比较......

还有一点注意事项:

array1 and array2 are two different sized arrays that contains thousands(~100.000)of strings in each.
also they are not in order. i would like to learn how to order them alphabetically.

1 个答案:

答案 0 :(得分:2)

使用该数据量,创建数据库,加载到表并使用SQL。尝试手动操作会太慢。