J
John Dann
Using VB2005, I have a class that retrieves some data from a file and
returns a datatable (typically 5000 - 50000 rows in size). The first
time this runs it works very quickly - small fraction of a second. But
if I call the same process again on the same data it now takes 2-3
seconds (5000 rows) to return and I can't see why this should be the
case.
In outline the class code is:
=================
Public class MyClass
Dim dt as New Datatable
Public function MakeDT(args) as datatable
dt.reset ' Ensure datatable is blank before reuse
' Process to create datatable schema
dt.columns.add("col1", GetType(Byte))
' etc for 65 columns
For i as integer = 0 to NumberRowBlocks
PopulateRowBlock
Next
Return dt
End Function
Private Function PopulateRowBlock()
For j as integer = 0 to RowsInBlock
Dim dr as DataRow = dt.NewRow
PopulateDataRow(i) ' Detailed code not shown
dt.Rows.Add(dr)
Next
End Function
End Class
==================
The context is that MyClass is instantiated in the calling code
elsewhere and then the public function MakeDT is called as required,
returning a different datatable each time dependent on the args
supplied.
Timing different steps reveals that the step that takes the time
second time round (and subsequently) is creating the schema.
I don't claim to understand the details of datatables too well. Maybe
using dt.reset isn't the best way to clear the datatable of previous
data/schema. Maybe I should declare the datatable inside MakeDT and
not at class level and pass the datatable to my PopulateRowBlock
function?
I'd like to understand what's going slow/wrong here. Any thoughts
please?
returns a datatable (typically 5000 - 50000 rows in size). The first
time this runs it works very quickly - small fraction of a second. But
if I call the same process again on the same data it now takes 2-3
seconds (5000 rows) to return and I can't see why this should be the
case.
In outline the class code is:
=================
Public class MyClass
Dim dt as New Datatable
Public function MakeDT(args) as datatable
dt.reset ' Ensure datatable is blank before reuse
' Process to create datatable schema
dt.columns.add("col1", GetType(Byte))
' etc for 65 columns
For i as integer = 0 to NumberRowBlocks
PopulateRowBlock
Next
Return dt
End Function
Private Function PopulateRowBlock()
For j as integer = 0 to RowsInBlock
Dim dr as DataRow = dt.NewRow
PopulateDataRow(i) ' Detailed code not shown
dt.Rows.Add(dr)
Next
End Function
End Class
==================
The context is that MyClass is instantiated in the calling code
elsewhere and then the public function MakeDT is called as required,
returning a different datatable each time dependent on the args
supplied.
Timing different steps reveals that the step that takes the time
second time round (and subsequently) is creating the schema.
I don't claim to understand the details of datatables too well. Maybe
using dt.reset isn't the best way to clear the datatable of previous
data/schema. Maybe I should declare the datatable inside MakeDT and
not at class level and pass the datatable to my PopulateRowBlock
function?
I'd like to understand what's going slow/wrong here. Any thoughts
please?