Page 1 of 2 12 LastLast
Results 1 to 40 of 54

Thread: Copying of one Array to another

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    27

    Copying of one Array to another

    As title

    Due to the large quantity amount of data inside the array, is the another method in doing this other than the use of a for loop?

    I know .net has the method of Array.copy but do vb6 has something similar to it?

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Copying of one Array to another

    Why can't you use a Loop? Or you just want to know another way?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    27

    Re: Copying of one Array to another

    It is possible to use a loop but the performance of it is not that good.
    looping will take some time for the copying process to complete as the amount of arrays required in copying and the huge data in each array

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Copying of one Array to another

    Yes, you can simply assign them without referencing an element.
    Code:
    Public Sub ArrayCopy()
        Dim Array1() As Long
        Dim Array2() As Long
        Dim i As Long
        
        ReDim Array1(9)
        ReDim Array2(9)
        For i = 0 To 9
            Array1(i) = i
        Next
        Array2 = Array1
        Debug.Print Array2(3)
        Erase Array1, Array2
    End Sub

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Copying of one Array to another

    It is worth pointing out that you can't bulk copy fixed arrays, which is one of several reasons I always use dynamic arrays.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    27

    Re: Copying of one Array to another

    I have got to know of an API called copymemory that does the trick, but I have yet to get it fully functioning

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Quote Originally Posted by dunno98
    It is possible to use a loop but the performance of it is not that good.
    looping will take some time for the copying process to complete as the amount of arrays required in copying and the huge data in each array
    Actually, looping is the fastest way in VB. If you loop VB will optimize an array loop and will copy a the array memory block to the new location and not loop at all.

    If you use the statement

    Array1 = Array2

    VB will do all sorts of bounds checking and structure checks thus slowing it down.

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Copying of one Array to another

    Quote Originally Posted by randem
    If you use the statement

    Array1 = Array2

    VB will do all sorts of bounds checking and structure checks thus slowing it down.
    Thanks for the heads up; that's good to know.

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,802

    Re: Copying of one Array to another

    Quote Originally Posted by dunno98
    I have got to know of an API called copymemory that does the trick, but I have yet to get it fully functioning
    vb Code:
    1. Option Explicit
    2. Option Base 0
    3.  
    4. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
    5.  
    6. Private Sub Form_Load()
    7.     Dim Arr() As Long
    8.     Dim Arr2() As Long
    9.    
    10.     ReDim Arr(100)
    11.    
    12.     ' copy the array
    13.     ReDim Arr2(UBound(Arr))
    14.     CopyMemory Arr2(0), Arr(0), (UBound(Arr) + 1) * LenB(Arr(0))
    15. End Sub

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    That's because of the different structures in the array. If it were a just a byte array it would work find all other arrays the item structures are stored differently.

  11. #11
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Copying of one Array to another

    Quote Originally Posted by randem
    <snip>VB will do all sorts of bounds checking and structure checks thus slowing it down.
    I was not so sure, at least I thought it would do the checks in both cases.

    I tested the following code both in the IDE and Compiled, I think this code is slightly biased towards using the for loop in that I predimension the array before hand.
    Code:
    Option Explicit
    Const ITERATIONS = 10000
    Const ARRAYUBOUND = 1023
    Dim OldArr() As Long
    Dim NewArr() As Long
    
    Public Sub setup()
    Dim i As Long
      ReDim OldArr(ARRAYUBOUND)
      ReDim NewArr(ARRAYUBOUND)
      For i = 0 To ARRAYUBOUND
        OldArr(i) = i
      Next i
    End Sub
    
    Sub TestCode1()
    Dim i As Long
      For i = 1 To ITERATIONS
        NewArr = OldArr
      Next i
    End Sub
    
    Sub TestCode2()
    Dim i As Long, ii As Long
      For i = 1 To ITERATIONS
        For ii = 0 To ARRAYUBOUND
          NewArr(ii) = OldArr(ii)
        Next ii
      Next i
    End Sub
    In both sets pink represents Array2=Array1 and green represents For... Array2(i)=Array1(i) ...Next. The setup code was not timed.

    In the IDE




    Compiled (with full compile optimisations for speed)

    Last edited by Milk; Jul 4th, 2007 at 07:01 PM.

  12. #12
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Post the project. I will tell you what it compiles to and verify.

  13. #13
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Copying of one Array to another

    Okay, I'm interested, that's why I tested.
    Attached Files Attached Files

  14. #14
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    For numbers array1=array2 is faster but for strings it is not. It's because of the way they are stored in memory.
    Attached Images Attached Images  

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Numbers can be stored and copied in a block of contiguous memory (they will always be the same size) however string cannot be stored that way and must be checked for length, unicode etc and cannot be moved in the same fashion.

  16. #16
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Quote Originally Posted by dunno98
    I have got to know of an API called copymemory that does the trick, but I have yet to get it fully functioning
    You will never get it fully functional for the reasons I have stated.

  17. #17
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    It seems array1=array2 is faster with everything except strings. I will check further.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    27

    Re: Copying of one Array to another

    [Quote from http://www.codeproject.com/vb/net/ne...orysample.asp]
    CopyMemory is a function that will copy the contents of one block of memory to a different block of memory without regard to the data type that is stored there. This results in an ultra fast copy of data, especially for objects like Structures, Classes and Strings.

    Isn't this true?
    Just interested to find out.

  19. #19
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Yes, but you must understand how VB stores and references the datatypes. Strings are variable in length so the length cannot be predetermined. You would need to use fixed length strings.

  20. #20
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Copying of one Array to another

    I thought array1 = array2 would have been faster in any case. Seeing as it does not depend on inbuilt VB- Functions.

    Also randem did you test the copymemory method?

  21. #21
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    I am doing that now...

  22. #22
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Copying of one Array to another

    Kk cool

  23. #23
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Copying of one Array to another

    Randem, in the interests of VB6, nice one!

  24. #24
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Always in the interest of VB6!!!. Good code Milk, I am modifying it a bit now for more test.

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    27

    Cool Re: Copying of one Array to another

    Quote Originally Posted by randem
    Yes, but you must understand how VB stores and references the datatypes. Strings are variable in length so the length cannot be predetermined. You would need to use fixed length strings.

    Cool,
    I learn something new everyday.

  26. #26
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Here are the results with copymemory...
    Attached Images Attached Images  

  27. #27
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Fixed Length Strings. Copy memory won't handle this properly as coded.
    Attached Images Attached Images  

  28. #28
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Variable Length Strings.
    Attached Images Attached Images  

  29. #29
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Object - Collection
    Attached Images Attached Images  

  30. #30
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Bytes
    Attached Images Attached Images  

  31. #31
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    Doubles
    Attached Images Attached Images  

  32. #32
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Copying of one Array to another

    Outstanding work, guys. Very informative thread.

  33. #33
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Copying of one Array to another

    Faster declarations of memory moving procedures: RtlMoveLong and vbaCopyBytes:
    Code:
    ' Module1.bas: if you rename, see AddressOf lines
    Option Explicit
    
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef lpvDest As Any, ByRef lpvSrc As Any, ByVal cbLen As Long)
    Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
    Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
    Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
    Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    
    Private Sub DeclareAPI(ByVal AddressOfDest As Long, ByRef API As String, ByRef Module As String)
        Dim lngModuleHandle As Long, AddressOfSrc As Long
        Dim larJMPASM(1) As Long
        Dim lngProcessHandle As Long, lngBytesWritten As Long
        ' get handle for module
        lngModuleHandle = GetModuleHandle(Module)
        If lngModuleHandle = 0 Then lngModuleHandle = LoadLibrary(Module)
        ' if failed, we can't do anything
        If lngModuleHandle = 0 Then Exit Sub
        ' get address of function
        AddressOfSrc = GetProcAddress(lngModuleHandle, API)
        ' if failed, we can't do anything
        If AddressOfSrc = 0 Then Exit Sub
        ' get a handle for current process
        lngProcessHandle = OpenProcess(&H1F0FFF, 0&, GetCurrentProcessId)
        ' if failed, we can't do anything
        If lngProcessHandle = 0 Then Exit Sub
        ' check if we are in the IDE
        If App.LogMode = 0 Then
            ' get the real location of the procedure
            CopyMemory AddressOfDest, ByVal AddressOfDest + &H16&, 4&
        End If
        ' set ASM JMP
        larJMPASM(0) = &HE9000000
        ' set JMP parameter (how many bytes to jump)
        larJMPASM(1) = AddressOfSrc - AddressOfDest - 5
        ' replace original procedure with the JMP
        WriteProcessMemory lngProcessHandle, ByVal AddressOfDest, ByVal VarPtr(larJMPASM(0)) + 3, 5, lngBytesWritten
        ' close handle for current process
        CloseHandle lngProcessHandle
    End Sub
    Public Sub RtlMoveLong(ByVal lpvDest As Long, ByVal lpvSrc As Long, ByVal cbLen As Long)
        DeclareAPI AddressOf Module1.RtlMoveLong, "RtlMoveMemory", "ntdll.dll"
        RtlMoveLong lpvDest, lpvSrc, cbLen
    End Sub
    Public Sub vbaCopyBytes(ByVal Length As Long, ByVal dest As Long, ByVal Src As Long)
        DeclareAPI AddressOf Module1.vbaCopyBytes, "__vbaCopyBytes", "msvbvm60.dll"
        vbaCopyBytes Length, dest, Src
    End Sub

  34. #34
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    And yet more data...
    Attached Images Attached Images  

  35. #35
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    And more... Not much of a difference though
    Attached Images Attached Images  

  36. #36
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    And more...
    Attached Images Attached Images  

  37. #37
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Copying of one Array to another

    even more...
    Attached Images Attached Images  

  38. #38
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Copying of one Array to another

    See what happens when you iterate a lot, but copy very little (as the trick mostly speeds up the call by preventing VB to do it's regular "wise" checks, thus improving processing of string data for example). With a lot of data there is no difference as it already works close to the maximum speed available.

  39. #39
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Copying of one Array to another

    One thing I'm struck by is how incredibly slow doubles are. I always knew they were, but those giant bars really drive it home.

    And as a corollary, how much faster singles are than doubles. Not much of a surprise that half the data size processes twice as fast, but again the big colored bars help with internalizing the concept.

  40. #40
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Copying of one Array to another

    Er, copymemory IS copying twice the amount of data there (32-bit single, 64-bit double, equal amount of items -> twice the data)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width