Why the difference in speed?

Posted by AngryHacker on Stack Overflow See other posts from Stack Overflow or by AngryHacker
Published on 2010-04-22T05:44:55Z Indexed on 2010/04/22 5:53 UTC
Read the original article Hit count: 238

Filed under:
|
|

Consider this code:

function Foo(ds as OtherDLL.BaseObj)
    dim lngRowIndex as long 
    dim lngColIndex as long

    for lngRowIndex = 1 to ubound(ds.Data, 2)
        for lngColIndex = 1 to ds.Columns.Count
            Debug.Print ds.Data(lngRowIndex, lngColIndex)
        next
    next
end function

OK, a little context. Parameter ds is of type OtherDLL.BaseObj which is defined in a referenced ActiveX DLL. ds.Data is a variant 2-dimensional array (one dimension carries the data, the other one carries the column index. ds.Columns is a Collection of columns in 'ds.Data`.

Assuming there are at least 400 rows of data and 25 columns, this code takes about 15 seconds to run on my machine. Kind of unbelievable.

However if I copy the variant array to a local variable, so:

function Foo(ds as OtherDLL.BaseObj)
    dim lngRowIndex as long 
    dim lngColIndex as long
    dim v as variant

    v = ds.Data
    for lngRowIndex = 1 to ubound(v, 2)
        for lngColIndex = 1 to ds.Columns.Count
            Debug.Print v(lngRowIndex, lngColIndex)
        next
    next
end function

the entire thing processes in barely any noticeable time (basically close to 0).

Why?

© Stack Overflow or respective owner

Related posts about vb6

Related posts about Performance