[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Looping through an array with FOR EACH


Out of interest to see just how bad time wise using for each was, I put together a little test and was quite surprised by the result:

Ranking of each type of iteration by time

1       For each hybrid Time= 0.758384959, Total=9000003 0.758384959073737
2       Index only      Time= 0.837044355, total=9000003 0.83704435499385
3       for each only   Time= 1.282145344, Total=9000003 1.28214534395374

The short program is below.
It look like a combination of both types together depending on the array structure is best!

-----------------------------------------------------------------------------------------------------------------------------------------------

' Gambas module file

Public Sub Main()

    Dim Ranking As New Collection(gb.ignorecase)
    Dim areas1 As New Integer[][]
    ' Lets add a bunch Of entries To the arrays
    For p As Integer = 0 To 3000000
        areas1.Add([1, 1, 1])
    Next

    ' just indexing
    Dim total As Long = 0
    Dim starttime As Float = Timer
    For i As Integer = 0 To areas1.max
        For j As Integer = 0 To areas1[i].max
            total += areas1[i][j]
        Next
    Next
    Dim indextime As Float = Timer - starttime
    Ranking.Add(indextime, Subst("Index only      Time=&1, total=&2 ", Format(indextime, "##.########0"), total))

    ' just for each this is slower
    total = 0
    starttime = Timer
    For Each areaA As Integer[] In areas1
        For Each entry As Integer In areaA
            total += entry
        Next
    Next
    Dim puretime As Float = Timer - starttime
    Ranking.Add(puretime, Subst("for each only   Time=&1, Total=&2", Format(puretime, "##.########0"), total))

    'hybrid this may be better
    total = 0
    StartTime = Timer
    For Each area As Integer[] In areas1
        For i = 0 To area.max
            total += area[i]
        Next
    Next
    Dim foreachtime As Float = Timer - starttime
    Ranking.Add(foreachtime, Subst("For each hybrid Time=&1, Total=&2", Format(foreachtime, "##.########0"), total))

    'Display the ranking
    Dim rankindex As Integer = 1
    Dim sortitout As String[] = Ranking.keys.Sort()
    Print "Ranking of each type by time"
    For Each s As String In sortitout
        Print rankindex, s, Ranking[s]
        Inc rankindex
    Next

End
' Gambas module file
_________________________________________________________________________________________________________________________________________

--
~~~~ Brian

Attachment: OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


Follow-Ups:
Re: Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
References:
Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBB <adamnt42@xxxxxxxxx>
Re: Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBrian G <brian@xxxxxxxxxxxxxxxx>
Re: Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
Re: Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBrian G <brian@xxxxxxxxxxxxxxxx>
Re: Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBruce Steers <bsteers4@xxxxxxxxx>