B4J Question cloning array of int

giggetto71

Active Member
Licensed User
Longtime User
Hi,
I need to find a way to "clone" an array of integers. I know from the documentation that I cannot use the B4XSerializator as it specifically does not support arrays of primitves types.
Unfortunately changing this array into a List would be nearly impossible due to the specific application. I also doubt I would be able to turn it into an array of objects as I really need int values (the array is a chess board and the elements are the pieces mapped to int values )
What could be another way to make a fast copy of an array? Execution Speed is really key in this application.
thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
What could be another way to make a fast copy of an array? Execution Speed is really key in this application.
It sounds like a "preoptimization" question.
B4X:
Sub AppStart (Args() As String)
    Dim arr(1000) As Int
    For i = 0 To arr.Length - 1
        arr(i) = Rnd(0, 1000000)
    Next
    Dim n As Long = DateTime.Now
    For i = 1 To 1000
        CopyArray(arr)
    Next
    Log(DateTime.Now - n)
End Sub

Sub CopyArray(arr() As Int) As Int()
    Dim n(arr.Length) As Int
    For i = 0 To n.Length - 1
        n(i) = arr(i)
    Next
    Return n
End Sub
Copying 1000 arrays with 1000 elements takes 5 milliseconds on my PC.
Copying 1000 arrays with 64 elements takes between 0 to 1 milliseconds.
 
Upvote 0
Top