Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

I'm seeing some unexpected behavior and hoping someone can explain it. I'm creating 10,000 instances each of an OleDbConnection, OleDbDataAdapter, and OleDbCommand using

Dim cn As New OleDbConnection(cnStr)
etc.

and it takes ~820 milliseconds

 

Then I'm creating 10,000 instances of each of those using

obj = Activator.CreateInstance(GetCurrentHash.Item(interfaceName)) 

where GetCurrentHash... returns a cached "Type" to create. This is only taking ~680 milliseconds.

 

I was expecting the opposite and CreateInstance to be slower, mainly because of the initial Type lookup and I'm also accepting a hashtable of properties/values to be dynamically assigned after the creation too. Can anyone explain the slower performance of "New"? It's not a big deal because I'm getting even better than what I was hoping for but I find it curious anyway.

--tim
  • *Experts*
Posted

Just a guess since no one else is answering...

 

Did you look at the IL? I wonder if it performed the GetCurrentHash.Item(interfaceName) one time outside of the loop and used that type in the CreateInstance call?

 

Also, did you time this multiple times to make sure it wasn't a fluke? I've never used the CreateInstance method, so I have no idea what it's doing versus a New.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • Leaders
Posted

Here ya go. I'm not sure if the CreateInstance makes much sense without everything but you might be able to make sense of it. The first two are essentially the tests that I'm using behind a simple little form, the CreateInstance is snipped from the DataProviderHelper class.

 

Here's the tests:

 

   Private Sub GenInstancesCustomInstance(ByVal count As Integer)
       Dim i As Integer
       Dim p As DataProviderHelper
       p = New DataProviderHelper()

       For i = 0 To count
           Dim cn As IDbConnection
           Dim cmd As IDbCommand
           Dim adp As IDbDataAdapter
           p.CurrentProvider = "System.Data.OleDb"
           Dim hsh As New Hashtable()
           hsh.Add("ConnectionString", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & GetDBPath() & ";")
           cn = p.CreateInstance("IDbConnection", hsh)
           cmd = p.CreateInstance("IDbCommand", Nothing)
           adp = p.CreateInstance("IDbDataAdapter", Nothing)

           cn = Nothing
           cmd = Nothing
           adp = Nothing
       Next
       p = Nothing
   End Sub

   Private Sub GenInstancesBuiltIn(ByVal count As Integer)
       Dim i As Integer
       For i = 0 To count
           Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & GetDBPath() & ";")
           Dim cmd As New OleDb.OleDbCommand()
           Dim adp As New OleDb.OleDbDataAdapter()
           cn = Nothing
           cmd = Nothing
           adp = Nothing
       Next
   End Sub

 

and here's the createinstance:

Public Shared Function CreateInstance(ByVal interfaceName As String, ByVal args As Hashtable) As Object
       Dim obj As Object
       Try
           obj = Activator.CreateInstance(GetCurrentHash().Item(interfaceName))
       Catch ex As Exception
           Throw New Exception("Interface: " & interfaceName & " is not implemented in " & m_strCurrentService, ex)
       End Try

       If Not args Is Nothing Then
           Dim en As IDictionaryEnumerator
           en = args.GetEnumerator()

           While en.MoveNext()
               Dim t As Type = obj.GetType()
               Dim pi As PropertyInfo = t.GetProperty(en.Key.ToString())
               If Not pi Is Nothing Then
                   pi.SetValue(obj, en.Value, Nothing)
               Else
                   Throw New Exception(en.Key.ToString() & " is not a property of " & obj.GetType().ToString())
               End If
           End While
       End If
       Return obj
   End Function

--tim

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...