Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Visual Basic

ArrayLists, Generic Lists, Is There A Way To Insert Items Faster?

Rate me:
Please Sign up or sign in to vote.
4.83/5 (37 votes)
26 Nov 2014CPOL6 min read 111.4K   588   101  
In this article, I try to highlight some issues in the .NET Framework generic list and how to circumvent them
Module Module1
    ' you can change these numbers if you want to test smaller/larger lists
    Private Const NB_ITEMS As Integer = 100000

    Sub Main()
        Menu()
    End Sub

    Sub Menu()
        Test("ArrayList", New ArrayList())
        Test("List(Of Integer)", New List(Of Integer))
        Test("LinkedArray", New LinkedArray())
        Test("LinkedArray(Of Integer)", New Generic.LinkedArray(Of Integer))
        Console.Write("Program completed. Press a key to exit")
        Console.ReadKey()
    End Sub

    Sub Test(ByVal TestName As String, ByVal l As IList)
        Console.WriteLine("*** Test speed of " & TestName & " ****")
        Dim t As DateTime = Now
        Append(l)
        RemoveLast(l)
        InsertFirst(l)
        RemoveFirst(l)
        InsertRandom(l)
        FetchItems(l)
        RunEnumerator(l)
        RemoveRandom(l)
        l.Clear()
        Console.Write("Total")
        WriteResult(Now.Subtract(t))
        Console.WriteLine()
    End Sub

    Private Sub WriteResult(ByVal ts As TimeSpan)
        Dim value As Double = ts.TotalSeconds
        Dim res As String = value.ToString("#,##0.000 sec")
        If Console.CursorLeft < 45 Then Console.Write(StrDup(45 - Console.CursorLeft, "."))
        If res.Length < 12 Then res = Space(12 - res.Length) & res
        Console.WriteLine(":" & res)
    End Sub

    Sub Append(ByVal l As IList)
        l.Clear()
        Console.Write("Append {0} items at the end", NB_ITEMS.ToString("#,##0"))
        Dim t As DateTime = Now
        For cpt As Integer = 1 To NB_ITEMS
            l.Add(cpt)
        Next
        WriteResult(Now.Subtract(t))
    End Sub

    Sub InsertFirst(ByVal l As IList)
        l.Clear()
        Console.Write("Insert {0} items at the beginning", NB_ITEMS.ToString("#,##0"))
        Dim t As DateTime = Now
        For cpt As Integer = 1 To NB_ITEMS
            l.Insert(0, cpt)
        Next
        WriteResult(Now.Subtract(t))
    End Sub

    Sub InsertRandom(ByVal l As IList)
        l.Clear()
        Console.Write("Insert {0} items at a random position", NB_ITEMS.ToString("#,##0"))
        Dim t As DateTime = Now
        Dim pos As Integer
        Dim rnd As New Random
        For cpt As Integer = 1 To NB_ITEMS
            pos = rnd.Next(l.Count)
            l.Insert(pos, cpt)
        Next
        WriteResult(Now.Subtract(t))
    End Sub

    Sub FetchItems(ByVal l As IList)
        l.Clear()
        Console.Write("Get {0} items", NB_ITEMS.ToString("#,##0"))
        For cpt As Integer = 1 To NB_ITEMS
            l.Add(cpt)
        Next
        Dim t As DateTime = Now
        For cpt As Integer = 1 To NB_ITEMS
            Dim result As Object = l.Item(cpt - 1)
        Next
        WriteResult(Now.Subtract(t))
    End Sub

    Sub RemoveLast(ByVal l As IList)
        Console.Write("Remove last item {0} times", NB_ITEMS.ToString("#,##0"))
        Dim t As DateTime = Now
        While l.Count > 0
            l.RemoveAt(l.Count - 1)
        End While
        WriteResult(Now.Subtract(t))
    End Sub


    Sub RemoveFirst(ByVal l As IList)
        Console.Write("Remove first object {0} times", NB_ITEMS.ToString("#,##0"))
        Dim t As DateTime = Now
        While l.Count > 0
            l.RemoveAt(0)
        End While
        WriteResult(Now.Subtract(t))
    End Sub

    Sub RemoveRandom(ByVal l As IList)
        Console.Write("Remove items randomly", NB_ITEMS.ToString("#,##0"))
        Dim t As DateTime = Now
        Dim rnd As New Random
        While l.Count > 0
            Dim idx As Integer = rnd.Next(l.Count)
            l.RemoveAt(idx)
        End While
        WriteResult(Now.Subtract(t))
    End Sub

    Sub RunEnumerator(ByVal l As IList)
        l.Clear()
        Console.Write("ForEach enumerator")
        For cpt As Integer = 1 To NB_ITEMS
            l.Add(cpt)
        Next
        Dim t As DateTime = Now
        For Each o As Object In l
            ' nothing
        Next
        WriteResult(Now.Subtract(t))
    End Sub

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions