Compare times to load sorted ListBoxes different ways

Started by nandagopal, May 20, 2009, 07:18 PM

Previous topic - Next topic

nandagopal

This example compares the times to load data into a ListBox with the form visible or not, and the ListBox sorted, not sorted, or sorted manually using quicksort. The result is that it is fastest to load using quicksort when the form is not yet visible.


Private Sub Form_Load()
Const NUM_VALUES = 10000
Dim i As Integer
Dim start_time As Single
Dim stop_time As Single
Dim values(1 To NUM_VALUES) As Integer

    Screen.MousePointer = vbHourglass

    ' Before the form is visible.
    start_time = Timer
    For i = 1 To NUM_VALUES
        List1.AddItem Format$(Rnd * 30000, "00000")
    Next i
    stop_time = Timer
    lblList1.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"

    start_time = Timer
    For i = 1 To NUM_VALUES
        List2.AddItem Format$(Rnd * 30000, "00000")
    Next i
    stop_time = Timer
    lblList2.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"

    start_time = Timer
    For i = 1 To NUM_VALUES
        values(i) = Rnd * 30000
    Next i
    Quicksort values, 1, 10000
    For i = 1 To NUM_VALUES
        List3.AddItem Format$(values(i), "00000")
    Next i
    stop_time = Timer
    lblList3.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"

    ' Show the form.
    Me.Show
    DoEvents

    start_time = Timer
    For i = 1 To NUM_VALUES
        List4.AddItem Format$(Rnd * 30000, "00000")
    Next i
    DoEvents
    stop_time = Timer
    lblList4.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"
    DoEvents

    start_time = Timer
    For i = 1 To NUM_VALUES
        List5.AddItem Format$(Rnd * 30000, "00000")
    Next i
    DoEvents
    stop_time = Timer
    lblList5.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"
    DoEvents

    start_time = Timer
    For i = 1 To NUM_VALUES
        values(i) = Rnd * 30000
    Next i
    Quicksort values, 1, 10000
    For i = 1 To NUM_VALUES
        List6.AddItem Format$(values(i), "00000")
    Next i
    DoEvents
    stop_time = Timer
    lblList6.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"
    DoEvents

    start_time = Timer
    List7.Visible = False
    For i = 1 To NUM_VALUES
        List7.AddItem Format$(Rnd * 30000, "00000")
    Next i
    List7.Visible = True
    DoEvents
    stop_time = Timer
    lblList7.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"

    Screen.MousePointer = vbDefault
End Sub