Object creation

quwiltw

Well-known member
Joined
Sep 18, 2001
Messages
486
Location
Washington, D.C.
Im seeing some unexpected behavior and hoping someone can explain it. Im creating 10,000 instances each of an OleDbConnection, OleDbDataAdapter, and OleDbCommand using
Code:
Dim cn As New OleDbConnection(cnStr)
etc.
and it takes ~820 milliseconds

Then Im creating 10,000 instances of each of those using
Code:
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 Im also accepting a hashtable of properties/values to be dynamically assigned after the creation too. Can anyone explain the slower performance of "New"? Its not a big deal because Im getting even better than what I was hoping for but I find it curious anyway.
 
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 wasnt a fluke? Ive never used the CreateInstance method, so I have no idea what its doing versus a New.

-Nerseus
 
Here ya go. Im 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 Im using behind a simple little form, the CreateInstance is snipped from the DataProviderHelper class.

Heres the tests:

Code:
    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 heres the createinstance:
Code:
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
 
Back
Top