Benchmark Source Code

The companion CD to this book contains all the source code needed to run and/or build the automation examples in this chapter. Both the Microsoft Visual Basic 4.0 Automation servers (InProc and OutProc folders) and the VBA source code (UsingOle.xls) are included. The complete VBA code is listed below:

Sub Button1_Click()
    ''' Early bind to objects
    Dim cOutProc As clsOutProc
    Dim cInProc As clsInProc
    ''' Late bind
    Dim oOutProc As Object
    Dim oInProc As Object
    Dim dStart As Date
    Dim dEnd As Date
    Dim iIter As Integer
    Dim iCount As Integer
    Dim szString1 As String
    Dim szString2 As String
    Dim szString3 As String
    Dim szRes As String
    

    ''' Instantiate objects
    Set cOutProc = New clsOutProc
    Set cInProc = New clsInProc
    ''' Get number of iterations and strings
    With ThisWorkbook.Sheets(1)
        iIter = .Range("Iterations").Value
        szString1 = .Range("String1").Value
        szString2 = .Range("String2").Value
        szString3 = .Range("String3").Value
    End With
    ''' Show what we are doing
    Application.StatusBar = "Early Bound InProc Running..."
    
    ''' Get start time. This is subject to the granularity
    ''' of Excel's Now() function. 
    ''' This is accurate only to the second
    ''' You may have to increase the iterations 
    ''' to get relevant comparisons
    dStart = Now()
    
    ''' Loop and call the object
    For iCount = 1 To iIter
        szRes = cInProc.ConcatStrings(szString1, _
            szString2, szString3)
    Next
    ''' Record ending time
    dEnd = Now()
    
    ''' Display difference
    ThisWorkbook.Sheets(1).Range("EarlyInProcTiming") _
        .Value = DateDiff("s", dStart, dEnd)
    
    ''' Display what we are doing
    Application.StatusBar = "Early Bound OutProc Running..."
    
    ''' Get next start time
    dStart = Now()
    
    ''' Loop and call the object
    For iCount = 1 To iIter
        szRes = cOutProc.ConcatStrings(szString1, _
        szString2, szString3)
    Next

    ''' Record ending time
    dEnd = Now()

    ''' Display difference
    ThisWorkbook.Sheets(1).Range("EarlyOutProcTiming") _
        .Value = DateDiff("s", dStart, dEnd)

    ''' now do the late bound versions
    ''' Instantiate objects
    Set oInProc = CreateObject("EDKInProcess.clsInProc")
    Set oOutProc = CreateObject("EDKOutProc.clsOutProc")
    
    ''' Display what we are doing
    Application.StatusBar = "Late Bound InProc Running..."

    ''' Record starting time
    dStart = Now()
    
    ''' Loop and call the object
    For iCount = 1 To iIter
        szRes = oInProc.ConcatStrings(szString1, _
     szString2, szString3)
    Next

    ''' Record ending time
    dEnd = Now()

    ''' Display difference
    ThisWorkbook.Sheets(1).Range("LateInProcTiming") _
        .Value = DateDiff("s", dStart, dEnd)

    ''' Display what we are doing    
    Application.StatusBar = "Late Bound OutProc Running..."

    ''' Get next start time
    dStart = Now()
    
    ''' Loop and call the object
    For iCount = 1 To iIter
        szRes = oOutProc.ConcatStrings(szString1, _
     szString2, szString3)
    Next

    ''' Record ending time
    dEnd = Now()
    
    ''' Display difference
    ThisWorkbook.Sheets(1).Range("LateOutProcTiming") _
        .Value = DateDiff("s", dStart, dEnd)
    

    ''' Clear the status bar
    Application.StatusBar = False

    ''' Release the objects
    Set oOutProc = Nothing
    Set oInProc = Nothing
    Set cInProc = Nothing
    Set cOutProc = Nothing
End Sub