Why is this code so slow?

Discussion in 'Visual Basic .NET' started by tb303, Jan 28, 2013.

  1. tb303

    tb303 Senior Member

    Joined:
    Dec 18, 2011
    Messages:
    962
    Likes Received:
    650
    Code:
    Dim num As Integer = DataSet1.Tables(0).Rows.Count - 1
    Dim no As String = "No"
    For n = num To 0 Step -1
         DataSet1.Tables(0).Rows(n).Item(2) = no
     Next
    
    It wasnt until i tried this on a datatable with 40k+ row that i realized how slow it was!
    It slows to a crawl and does only 2 loops or so a second (i have a i7 @ 4500Mhz)

    Hopefully you're not going to say dont use a datatable as the whole program is built around that datatable and id hate to start again over this final stumbling block for me!!

    by the way I tried it like this originally as well...
    Code:
    For Each dr As DataRow In DataSet1.Tables(0).Rows
         dr("Tested") = "No"
    Next
    
    hope someone can help :)
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,907
    Likes Received:
    13,014
    Occupation:
    Potentate
    Location:
    Asuncion
    • Thanks Thanks x 1
    Last edited: Jan 28, 2013
  3. tb303

    tb303 Senior Member

    Joined:
    Dec 18, 2011
    Messages:
    962
    Likes Received:
    650
    perfect. i hoped i was missing something simple.

    thanks, you've taught me something very useful there:)
     
    • Thanks Thanks x 1