Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 用Excel清除VBA中对象数组的性能问题_Arrays_Excel_Vba - Fatal编程技术网

Arrays 用Excel清除VBA中对象数组的性能问题

Arrays 用Excel清除VBA中对象数组的性能问题,arrays,excel,vba,Arrays,Excel,Vba,我在使用Excel清除VBA中的大型对象数组时遇到性能问题。数组中的对象是包含许多属性的自定义类的实例。代码的简化版本如下所示: Sub SlowExampleDynamic() 'Create an array consisting of a large number of objects with a user defined class. Dim ExampleArray() As CustomClass Dim i As Long For i = 0

我在使用Excel清除VBA中的大型对象数组时遇到性能问题。数组中的对象是包含许多属性的自定义类的实例。代码的简化版本如下所示:

Sub SlowExampleDynamic()
    'Create an array consisting of a large number of objects with a user defined class.
    Dim ExampleArray() As CustomClass
    Dim i As Long  
    For i = 0 To 100000
        'Make a user defined class in memory at each array position
        ReDim Preserve ExampleArray(i)
        Set ExampleArray(i) = New CustomClass
        'Populate the user defined class with data
        Set ExampleArray(i) = GenerateClassData()
    Next i
    
    'The following line takes upwards of 5 minutes
    Erase ExampleArray
   
End Sub 
该行的擦除示例数组花费了5分钟以上的时间

我尝试用静态数组实现这一点,并清除循环中的元素,但得到了相同的结果:

Sub SlowStaticExample()
    'Create an array consisting of a large number of objects with a user defined class.
    Dim ExampleArray(100000) As CustomClass
    Dim i As Long  
    For i = 0 To 100000
        'Make a user defined class in memory at each array position
        Set ExampleArray(i) = New CustomClass
        'Populate the user defined class with data
        Set ExampleArray(i) = GenerateClassData()
    Next i
   
    'The following loop takes upwards of 5 minutes
    For i = 0 To 100000
        Set ExampleArray(i) = Nothing
    Next i
End Sub
但是,如果我运行与上面相同的代码,但实际上从未使用数据填充CustomClass对象,则数组将在不到一秒钟内清除:

Sub FastExample()
    'Create an array consisting of a large number of objects with a user defined class.
    Dim ExampleArray(100000) As CustomClass
    Dim i As Long  
    For i = 0 To 100000
        'make a user defined class in memory at each array position
        Set ExampleArray(i) = New CustomClass
    Next i
   
    'The following loop will run in less than a second
    For i = 0 To 100000
        Set ExampleArray(i) = Nothing
    Next i
End Sub
我正在进行部分填充CustomClass对象的测试,以尝试确定断点,但到目前为止,这种方法没有成功


什么会导致清除填充的自定义对象数组的过程花费如此长的时间?

类属性是否都是非对象类型,或者类实例中是否存在嵌套对象?其中两个类属性是自定义对象。这两个自定义对象中的每一个都只有非对象特性。因此,有一个嵌套对象的级别。我认为您遇到了VBA和“经典”VB(来自VB6)中的一个已知问题,即销毁对象非常慢(与VB5相比)