Monday, July 27, 2009

Analysis of Typed VS UnTyped DataSet

1. Create a project with name Analysis.

2. Follow the steps provided in this link to create typed dataset http://support.microsoft.com/kb/320714

2. Create three buttons with name “Typed”, “Untyped” and “Difference” on the Form.

3. Declare two class level variable

4. Copy and paste below code in “Typed” method.


Private Sub Typed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         Dim watch As Stopwatch = New Stopwatch()
         watch = Stopwatch.StartNew()
         Dim cn As SqlConnection = New SqlConnection("ConnectionString")
         Dim cmd As SqlCommand = New SqlCommand("select * from [Alphabetical list of products]", cn)   
         Dim
da As SqlDataAdapter = New SqlDataAdapter(cmd)
         Dim tds As dsProducts = New dsProducts()
         da.Fill(tds, tds.Tables(0).TableName)
         Dim rowCount As Integer = tds.Tables(0).Rows.Count
         Dim i As Integer
         Dim str As String = String.Empty
         Dim intValue As Integer = 0
         Dim sortValue As Double = 0
         Dim bln As Boolean
         For i = 1 To rowCount
                  intValue = tds.Alphabetical_list_of_products(0).ProductID
                  intValue = tds.Alphabetical_list_of_products(0).SupplierID
                  intValue = tds.Alphabetical_list_of_products(0).CategoryID
                  str = tds.Alphabetical_list_of_products(0).CategoryName
                  str = tds.Alphabetical_list_of_products(0).ProductName
                  str = tds.Alphabetical_list_of_products(0).QuantityPerUnit
                  sortValue = tds.Alphabetical_list_of_products(0).UnitPrice
                  intValue = tds.Alphabetical_list_of_products(0).UnitsInStock
                  intValue = tds.Alphabetical_list_of_products(0).UnitsOnOrder
                  intValue = tds.Alphabetical_list_of_products(0).ReorderLevel
                  bln = tds.Alphabetical_list_of_products(0).Discontinued
        
Next
         tds.Dispose()
         watch.Stop()
         TypedDataset = watch.ElapsedTicks
         MessageBox.Show(
"Typed " + Convert.ToString(TypedDataset))
End Sub



5. Copy and paste below code in “UnTyped” method.


Private Sub UnTyped_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
         Dim watch As Stopwatch = New Stopwatch()
         watch = Stopwatch.StartNew()
         Dim cn As SqlConnection = New SqlConnection("ConnectionString")
         Dim cmd As SqlCommand = New SqlCommand("select * from [Alphabetical list of products]", cn)        
         Dim
da As SqlDataAdapter = New SqlDataAdapter(cmd)
        
Dim tds As DataSet = New DataSet()
         da.Fill(tds)
        
Dim rowCount As Integer = tds.Tables(0).Rows.Count
        
Dim i As Integer
        
Dim str As String = String.Empty
        
Dim intValue As Integer = 0
        
Dim sortValue As Double = 0
        
Dim bln As Boolean
        
For i = 0 To rowCount - 1
                  intValue = Convert.ToInt32(tds.Tables(0).Rows(i).Item(
"ProductID"))
                  intValue = Convert.ToInt32(tds.Tables(0).Rows(i).Item(
"SupplierID"))
                  intValue = Convert.ToInt32(tds.Tables(0).Rows(i).Item(
"CategoryID"))
                  str = tds.Tables(0).Rows(i).Item(
"CategoryName").ToString()
                  str = tds.Tables(0).Rows(i).Item(
"ProductName").ToString()
                  str = tds.Tables(0).Rows(i).Item(
"QuantityPerUnit").ToString()
                  intValue = Convert.ToInt32(tds.Tables(0).Rows(i).Item(
"UnitPrice"))
                  intValue = Convert.ToInt32(tds.Tables(0).Rows(i).Item(
"UnitsInStock"))
                  intValue = Convert.ToInt32(tds.Tables(0).Rows(i).Item(
"UnitsOnOrder"))
                  intValue = Convert.ToInt32(tds.Tables(0).Rows(i).Item(
"ReorderLevel"))
                  bln = Convert.ToBoolean(tds.Tables(0).Rows(i).Item(
"Discontinued"))
        
Next
        
tds.Dispose()
         watch.Stop()
         UnTypedDataset = watch.ElapsedTicks
         MessageBox.Show(
"Untyped " + Convert.ToString(UnTypedDataset))
End Sub






6. Copy and paste below code in “Difference” method.

Private Sub Difference_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
         MessageBox.Show(
"Difference " + Convert.ToString(TypedDataset - UnTypedDataset))
End Sub


7. Now, we are all set to run application.

8. Click on type and un-typed.

9. Now click on difference, you will see the difference of execution time between typed and un-typed.

10.When you run the above code first time, you will notice that Typed dataset take 50-70 times more compare to un-typed dataset, it is because typed dataset read the xsd file and keep it in the memory for reference.

Now let’s have a look on the memory consumption and other things, it will give more clear understanding.

Typed DataSet - Summary


Untyped DataSet - Summary


In above two diagrams you can see the difference in Heap Statistics, Garbage Collection and GC Handles Statistics.

Typed DataSet – Allocated Bytes in Heap


Untyped DataSet-Allocated bytes in heap


In above diagram check right top, for typed total byte allocation is 842,423 and for un-typed byte allocation is 522,144.

Typed DataSet – By Age


Untyped DataSet – By Age


In above two diagrams, lifetime of objects in typed dataset is almost 25 sec and the age of un-typed is around 5 sec.

Typed DataSet – Object by address


Untyped DataSet - Objects by address


In above two diagram, typed has gen 0 and gen 1 which means garbage collector will run twice to clean it where as in un-typed it will run only once. Typed has also LOH which is large object heap.


Typed DataSet - Time Line


Untyped DataSet – Time Line


In the above diagram, typed dataset objects survive garbage collection and are prompted to next generation whereas in un-typed it is done in one generation only.

 Points to consider to choose between Typed DataSet and Untyped DataSet


Typed DataSet


·   It provides additional methods, properties and events and thus it makes it easier to use.


·   You will get type mismatch and other errors at compile time.


·   You will get advantage of intelliSense in VS. NET


·   Performance is slower in case of strongly typed dataset.


·   Typed DataSet has a schema


·   In complex environment, strongly typed dataset's are difficult to administer.


Untyped DataSet


·      It is not as easy to use as strongly typed dataset.


·      You will get type mismatch and other errors at runtime.


·      You can't get an advantage of intelliSense.


·      Performance is faster in case of Untyped dataset.


·      An Untyped DataSet does not have schema


·      Untyped datasets are easy to administer.

No comments:

Site Meter