title: How do I improve perfomance creating DataTable in VB.net?
body: <p>I have following code and it's slow to load first time. CSV file is about 4mb 16000 rows.</p>
  
  <pre><code>        If Session("tb") Is Nothing Then
              Dim str As String()
              If (IsNothing(Cache("csvdata"))) Then
                  str = File.ReadAllLines(Server.MapPath("~/test/feed.csv"))
                  Cache.Insert("csvdata", str, Nothing, DateTime.Now.AddHours(12), TimeSpan.Zero)
              Else
                  str = CType(Cache("csvdata"), Array)
              End If
              Dim dt As New DataTable
              dt.Columns.Add("Shape", GetType(System.String))
              dt.Columns.Add("Weight", GetType(System.Double))
              dt.Columns.Add("Color", GetType(System.String))
              dt.Columns.Add("Clarity", GetType(System.String))
              dt.Columns.Add("Price", GetType(System.Int32))
              dt.Columns.Add("CutGrade", GetType(System.String))
  
              For i As Integer = 1 To str.Length - 1
                  Dim pattern As String = ",(?=([^""]*""[^""]*"")*[^""]*$)"
                  Dim rgx As New Regex(pattern)
                  Dim t As String = rgx.Replace(str(i), "\")
                  Dim s As String() = t.Split("\"c)
                  Dim pr As Int32 = CType(s(5), Int32)
                  Dim fpr As Int32
                  Dim rate As Double
                  Select Case pr
                      Case Is &lt; 300
                          rate = 2
                      Case 301 To 600
                          rate = 1.7
                      Case Is &gt; 600
                          rate = 1.16
                  End Select
                  fpr = Math.Round(pr * rate)
                  Dim a As String() = {s(1), s(2), s(3), s(4), fpr, s(40)}
                  dt.Rows.Add(a)
              Next
  
              Session("tb") = dt
              ListView1.DataSource = dt
              ListView1.DataBind()
          Else
              Dim x As DataTable = CType(Session("tb"), DataTable)
              ListView1.DataSource = x
              ListView1.DataBind()
          End If
  </code></pre>
  
  <p>csv file is cached and I assume this can share with everyone. 
  (one person loads once in 12 hours)
  Once I create Session, the page loads fast as well.
  So, creating Datatable seems to be the slow process. 
  This is first time to deal with datatable and I'm sure someone can point out what I'm doing wrong.</p>
  
  <p>Thank you</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I have changed Cache to the original Datatable instead of CSV file.
  It loads fast now, but I would like to know if this is a bad idea or not.</p>
  
  <pre><code> Cache.Insert("csvdata", dt, Nothing, DateTime.Now.AddHours(12), TimeSpan.Zero)
  </code></pre>
  
  <p>Once it's stored in Cache, I can run Query against it using Linq.</p>
  
  <p><strong>SAMPLE CSV</strong> first 3 rows</p>
  
  <pre><code>Supplier ID,Shape,Weight,Color,Clarity,Price / Carat,Lot Number,Stock Number,Lab,Cert #,Certificate Image,2nd Image,Dimension,Depth %,Table %,Crown Angle,Crown %,Pavilion Angle,Pavilion %,Girdle Thinnest,Girdle Thickest,Girdle %,Culet Size,Culet Condition,Polish,Symmetry,Fluor Color,Fluor Intensity,Enhancements,Remarks,Availability,Is Active,FC-Main Body,FC- Intensity,FC- Overtone,Matched Pair,Separable,Matching Stock #,Pavilion,Syndication,Cut Grade,External Url
  9349,Round,1.74,F,VVS1,13650.00,,IM-95-188-243,ABC,11228,,,7.81|7.85|4.62,59.00,62.00,34.00,13.00,,,Medium,,0,None,,Excellent,Very Good,Blue,Medium,,"",Not Specified,Y,,,,False,True,,,,Very Good,http://www.test/teste.
  9949,Round,1.00,I,VVS1,6059.00,,IM-95-189-C021,ABC,212197,,,6.37|6.42|3.96,61.90,54.00,34.50,16.00,,,Thin,Slightly Thick,0,None,,Excellent,Good,,None,,"Additional pinpoints are not shown.",Guaranteed Available,Y,,,,False,True,,,,Very Good,http://www.test/test.
  </code></pre>