Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Code problem or exceeded array limits?

Code problem or exceeded array limits?

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpdata-structuresperformancequestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hurricane3000
    wrote on last edited by
    #1

    Hi all, I have a little problem with a pair of arrays, when them are involved in a comparation work: In particular, I have an Array A As String and another array B As String. Normally the program works with a single array A, but sometimes I need a second array B. Array A is filled with numerical string (sometime with up 15000 elements). Array B is filled with numerical string (sometime with up 15000 elements). The code works in this way: Each element of array B is compared with Each element of array A. If a value of array B is = to some value in array A then the value in the array B is removed and the array is resized (progressively size-decreased) but preserving remaining different values (Redim Preserve). In this way, at the end of comparation loop, the array B will contain only values that are not already contained in array A. The code seems to be perfectly working until the number of elements in array A and B is down about 2200 + 2200 elements, but if numbers of arrays element go up, the program seems to work (in an infinite loop) but in reality it is quited (closed) from O.S. In the fact the program name disappears from list of running program. The following is the schematic code:

    Do Until IndexB = ArrayB.GetLength(0)
    For IndexA = 1 to ArrayA.GetLength(0)
    If Val(ArrayB(IndexB)) = Val(ArrayA(IndexA)) Then
    ArrayB(IndexB) = ArrayB(ArrayB.GetLength(0) - 1)
    ReDim Preserve ArrayB((ArrayB.GetLength(0) - 1) - 1)
    IndexB = IndexB - 1
    Exit For
    End If
    Next
    IndexB = IndexB + 1
    Loop

    Application is running in a Windows Mobile 6.1 Device. Apparentely seems to be a memory problem, but I don't think so, because size of arrays is not exagerate and the device have enough available memory. Thanks for help.

    L G 2 Replies Last reply
    0
    • H Hurricane3000

      Hi all, I have a little problem with a pair of arrays, when them are involved in a comparation work: In particular, I have an Array A As String and another array B As String. Normally the program works with a single array A, but sometimes I need a second array B. Array A is filled with numerical string (sometime with up 15000 elements). Array B is filled with numerical string (sometime with up 15000 elements). The code works in this way: Each element of array B is compared with Each element of array A. If a value of array B is = to some value in array A then the value in the array B is removed and the array is resized (progressively size-decreased) but preserving remaining different values (Redim Preserve). In this way, at the end of comparation loop, the array B will contain only values that are not already contained in array A. The code seems to be perfectly working until the number of elements in array A and B is down about 2200 + 2200 elements, but if numbers of arrays element go up, the program seems to work (in an infinite loop) but in reality it is quited (closed) from O.S. In the fact the program name disappears from list of running program. The following is the schematic code:

      Do Until IndexB = ArrayB.GetLength(0)
      For IndexA = 1 to ArrayA.GetLength(0)
      If Val(ArrayB(IndexB)) = Val(ArrayA(IndexA)) Then
      ArrayB(IndexB) = ArrayB(ArrayB.GetLength(0) - 1)
      ReDim Preserve ArrayB((ArrayB.GetLength(0) - 1) - 1)
      IndexB = IndexB - 1
      Exit For
      End If
      Next
      IndexB = IndexB + 1
      Loop

      Application is running in a Windows Mobile 6.1 Device. Apparentely seems to be a memory problem, but I don't think so, because size of arrays is not exagerate and the device have enough available memory. Thanks for help.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this is horribly inefficient for many reasons: - you are converting strings to numbers all the time; it they really are numbers, store them in a numeric data type, not string. - ReDim Preserve is expensive as it has to allocate a new array, then copy all the data. When the number of items in a collection varies, you should consider using a real collection instead of an array; use a collection type if those are available, or create your own linked list structures. - For two arrays of size S1 and S2, you are performing S1*S2 comparisons. It would be cheaper to sort the first array, sort the second array, and then perform the equivalent of a merge-sort on both. This will improve speed dramatically and reduce the memory load drastically. So throw it all out; start using lists of numbers; then sort and merge. It would handle millions of numbers in a matter of seconds. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
      All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


      modified on Friday, February 26, 2010 1:50 PM

      1 Reply Last reply
      0
      • H Hurricane3000

        Hi all, I have a little problem with a pair of arrays, when them are involved in a comparation work: In particular, I have an Array A As String and another array B As String. Normally the program works with a single array A, but sometimes I need a second array B. Array A is filled with numerical string (sometime with up 15000 elements). Array B is filled with numerical string (sometime with up 15000 elements). The code works in this way: Each element of array B is compared with Each element of array A. If a value of array B is = to some value in array A then the value in the array B is removed and the array is resized (progressively size-decreased) but preserving remaining different values (Redim Preserve). In this way, at the end of comparation loop, the array B will contain only values that are not already contained in array A. The code seems to be perfectly working until the number of elements in array A and B is down about 2200 + 2200 elements, but if numbers of arrays element go up, the program seems to work (in an infinite loop) but in reality it is quited (closed) from O.S. In the fact the program name disappears from list of running program. The following is the schematic code:

        Do Until IndexB = ArrayB.GetLength(0)
        For IndexA = 1 to ArrayA.GetLength(0)
        If Val(ArrayB(IndexB)) = Val(ArrayA(IndexA)) Then
        ArrayB(IndexB) = ArrayB(ArrayB.GetLength(0) - 1)
        ReDim Preserve ArrayB((ArrayB.GetLength(0) - 1) - 1)
        IndexB = IndexB - 1
        Exit For
        End If
        Next
        IndexB = IndexB + 1
        Loop

        Application is running in a Windows Mobile 6.1 Device. Apparentely seems to be a memory problem, but I don't think so, because size of arrays is not exagerate and the device have enough available memory. Thanks for help.

        G Offline
        G Offline
        Gideon Engelberth
        wrote on last edited by
        #3

        As an alternative to the sort and merge approach, you may also try using a Dictionary/HashSet. If you have .NET 3.5 available, you could use the Except extension method (which I understand uses a HashSet internally) like so:

        Dim temp = ArrayB.Select(AddressOf Double.Parse) _
        .Except(ArrayA.Select(AddressOf Double.Parse)) _
        .ToArray()

        If you don't have the LINQ methods, you could do this yourself:

        Dim parsedToRemove As New Dictionary(Of Double, Double)
        Dim noDuplicates As New List(Of Double)

        For Each parsedA In Array.ConvertAll(ArrayA, AddressOf Double.Parse)
        If Not parsedToRemove.ContainsKey(parsedA) Then
        parsedToRemove.Add(parsedA, parsedA)
        End If
        Next
        For Each parsedB In Array.ConvertAll(ArrayB, AddressOf Double.Parse)
        If Not parsedToRemove.ContainsKey(parsedB) Then
        noDuplicates.Add(parsedB)
        End If
        Next
        'noDuplicates now has the values in B not in A

        H 1 Reply Last reply
        0
        • G Gideon Engelberth

          As an alternative to the sort and merge approach, you may also try using a Dictionary/HashSet. If you have .NET 3.5 available, you could use the Except extension method (which I understand uses a HashSet internally) like so:

          Dim temp = ArrayB.Select(AddressOf Double.Parse) _
          .Except(ArrayA.Select(AddressOf Double.Parse)) _
          .ToArray()

          If you don't have the LINQ methods, you could do this yourself:

          Dim parsedToRemove As New Dictionary(Of Double, Double)
          Dim noDuplicates As New List(Of Double)

          For Each parsedA In Array.ConvertAll(ArrayA, AddressOf Double.Parse)
          If Not parsedToRemove.ContainsKey(parsedA) Then
          parsedToRemove.Add(parsedA, parsedA)
          End If
          Next
          For Each parsedB In Array.ConvertAll(ArrayB, AddressOf Double.Parse)
          If Not parsedToRemove.ContainsKey(parsedB) Then
          noDuplicates.Add(parsedB)
          End If
          Next
          'noDuplicates now has the values in B not in A

          H Offline
          H Offline
          Hurricane3000
          wrote on last edited by
          #4

          Thanks for replies. Both contains good ideas to resolving the problem.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups