4 Replies - 16029 Views - Last Post: 28 June 2012 - 08:37 PM

#1 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Performance test of arraylist vs list of T

Posted 26 June 2012 - 10:55 AM

Hey all, So when program i usually use arraylists when working with data but whilst reading through various post on DIC i keep seeing the mod's, experts and frequent users telling people to use list(of T) and i was always curious as to why. So i decided to test both of them for performance (inserting 100000 integers). The results came out that list(of T) is over twice as fast as arraylists (code and results below). So i am wondering is my test fair, is list(of T) twice as fast at inserting compare to arraylists. If you think my test is unfair in anyway please explain.

Code:

    Sub Main()
        List_Of_T()
        arr_list_Test()
        System.Threading.Thread.Sleep(1000000000)
    End Sub


    Dim StopWatch As New Stopwatch

    Public Sub arr_list_Test()
        StopWatch.Start()
        Dim arr_Test As New ArrayList
        For i As Integer = 0 To 100000
            arr_Test.Add(i)
        Next
        StopWatch.Stop()
        Console.WriteLine(StopWatch.Elapsed.ToString)
    End Sub


    Public Sub List_Of_T()
        StopWatch.Start()
        Dim Lis_Test As New List(Of Integer)
        For i As Integer = 0 To 100000
            Lis_Test.Add(i)
        Next
        StopWatch.Stop()
        Console.WriteLine(StopWatch.Elapsed.ToString)
    End Sub


results :

List (of T) : 4 runs

00:00:00.0018848
00:00:00.0018781
00:00:00.0019760
00:00:00.0018498

average : 00:00:00.0018971.75

Arraylist : 4 runs

00:00:00.0052716
00:00:00.0048854
00:00:00.0054924
00:00:00.0039683

average : 00:00:00.0049044.25

PS Did not know if this should be in advanced or the general vb.net forum

Is This A Good Question/Topic? 1
  • +

Replies To: Performance test of arraylist vs list of T

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9090
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Performance test of arraylist vs list of T

Posted 26 June 2012 - 11:03 AM

Yup - plenty of information on the behind the scenes fun of why arraylists are less than optimal..

http://blog.drorhelp...-arraylist.html

http://allantech.blo...st-vs-list.html
Was This Post Helpful? 1
  • +
  • -

#3 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: Performance test of arraylist vs list of T

Posted 26 June 2012 - 11:09 AM

documentation from msdn says:

Quote

An Array of a specific type (other than Object) provides better performance than an ArrayList. This is because the elements of ArrayList are of type Object; therefore, boxing and unboxing typically occur when you store or retrieve a value type. However, a List(Of T) can provide performance similar to an array of the same type if no reallocations are required; that is, if the initial capacity is a good approximation of the maximum size of the list.

ArrayList and List Collection Types
Was This Post Helpful? 0
  • +
  • -

#4 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Performance test of arraylist vs list of T

Posted 26 June 2012 - 11:44 AM

Nice articles modi123 I must remember to use lists instead of arraylists i hadn't a clue about the performance difference between the two and also about how the cache deals with both.

Ps modi123 do you know of any good articles on what boxing and unboxing is

This post has been edited by m_wylie85: 26 June 2012 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Performance test of arraylist vs list of T

Posted 28 June 2012 - 08:37 PM

Simple example:

Dim custClass As New customClass
'boxing
TakesAnObject(custClass)


Private Sub TakesAnObject(source As Object)
  'unboxing makes the conversion back to it's original type
  Dim cc As customClass = CType(source, customClass)
End Sub


All of the controls events are done like this...
(sender As Object, e As EventArgs)
...of course with diff signatures.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1