Efficient VB.net memory function

I get memory exceptions from the following function when RowCollection is 50000+, and therefore I need to make it more memory efficient. The function simply needs to build a comma-delimited string of row indices stored in the RowCollection. Can anyone detect any obvious hunger operations in memory in the following?

NB RowCollection simply contains a list of row indices stored as integers.

 Private Function GetCommaSeparatedString(ByRef RowIndexes As ArrayList) As String
        Dim RowString As String = String.Empty

        'Build a string of the row indexes 
        'Add one onto each index value so our indexes begin at 1 
        For Each Row In RowIndexes 
            RowString += CInt(Row.ToString) + 1 & ","
        Next

        'Remove the last comma
        If RowString.Length > 0 Then
            RowString = RowString.Substring(0, RowString.Length - 1)
        End If

        Return RowString
    End Function

Thanks in advance.

+3
source share
4 answers

, , , .

, . StringBuilder , .

, StringBuilder - , , : String.Join. LINQ- -, :

Private Function GetCommaSeparatedString(ByVal RowIndexes As ArrayList) As String
    Return String.Join(",", From index In RowIndexes Select CInt(index) + 1)
End Function

, . RowIndexes, . , ToString() - , . ? CInt.

+4

: , Strilanc

, ( , ), StringBuilder, . , ( )

Private Function GetCommaSeparatedString(ByRef RowIndexes As ArrayList) As String
    Dim RowString As New StringBuilder()

    'Build a string of the row indexes 
    'Add one onto each index value so our indexes begin at 1 
    For Each Row In RowIndexes 
        RowString.AppendFormat("{0},",  CInt(Row.ToString) + 1)
    Next

    'Remove the last comma
    If RowString.Length > 0 Then
        RowString.Append(RowString.Substring(0, RowString.Length - 1))
    End If

    Return RowString
End Function
+3

StringBuilder - , , ?

+2

, 2 , .

"1,2,3,4,5,.... 499500" "1,2,3,4,5,.... 499500"

500 2 2000 , ( ).

( 1 50000) 100 000 , , . , ~ 10 000 000 000 ( , 2 /char) 20 .

StringBuilder += (RowString).
Ex

Dim RowString As StringBuilder = new StringBuilder( 100000 )

For Each Row In RowIndexes 
    RowString.Append( CInt(Row.ToString) + 1).Append( "," )
Next

'...'

Return RowString.ToString

, .

Private Function GetCommaSeperatedString(ByRef RowIndexes As ArrayList) As String
    Dim indexArray as String[] = RowIndexes
                                 .Select(Function(r)=> (CInt(r.ToString) + 1)ToString)
                                 .ToArray
    return String.Join( ',', indexArray)
End Function



* note: these are the first lines of VB that I have ever written, so I may have made a basic mistake (especially in linq / lambda materials), but the point is.

+1
source

Source: https://habr.com/ru/post/1762486/


All Articles