+ Reply to Thread
Results 1 to 3 of 3

Variant vs Array

Hybrid View

  1. #1
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Variant vs Array

    Quick question, for the below, the second loop is faster than the first.

    Is this due to the overhead associated with the Variant type? - Granted it's not much slower though

    Sub DirectArray()
    Dim arr(1 To 10000) As Long
    Dim rngArr
    Dim x As Long
    Dim y As Long
    Dim t As Class1
    
    
    For x = 1 To 10000
        arr(x) = x
    Next x
    
    
    rngArr = Application.Transpose(Sheet2.Range("A1:A10000").Value)
    
    
    Set t = New Class1
    With t
        .StartCounter
        For x = 1 To 10000
            y = rngArr(x) + y
        Next x
        Debug.Print "rngArr: " & .TimeElapsed
        
        y = 0
        
        .StartCounter
        For x = 1 To 10000
            y = arr(x) + y
        Next x
        Debug.Print "arr: " & .TimeElapsed
    End With
    End Sub
    Last edited by Kyle123; 01-26-2012 at 09:50 AM.

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Variant vs Array

    Yes - you are having to coerce data types to do the arithmetic.
    Good luck.

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Variant vs Array

    Cheers, that's what I thought

+ 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