Benchmarks: Excel automation

Ce document est disponible en français


VBA code that drives Excel through its object model, late-bound: a block of cells read into a Variant at once, worked on, and written to another sheet at once, then the same work done cell by cell. Each case gives the time of each phase on its own.

The source is the module ExcelCases.bas, embedded in rdc. These cases need Excel, so they are measured in the reference run only (see Measuring MS-VBA), and their badges give the reference run's date instead of a live figure. Each badge gives RD-VBA's time as a multiple of MS-VBA's (how the figures are measured).

How the cases run

With rdc bench-msvba, both sides run the cases against an Excel instance of their own, in another process, so that every call to Excel's object model crosses a process boundary. RD-VBA runs them from the program Program.bas, which starts that instance and gives the number of runs from the command line:

Public Sub Main()
    Dim Xl As Object
    Set Xl = CreateObject("Excel.Application")
    Xl.Visible = False
    Xl.DisplayAlerts = False
    Debug.Print RunExcelCases(Xl, CLng(Command$))
    Xl.Quit
End Sub

MS-VBA runs RunExcelCases from a workbook in Excel, against a second Excel instance it starts. RunExcelCases turns off screen updating, events and automatic calculation, fills a sheet with 100,000 × 10 cells, and runs the cases in this order:

Public Function RunExcelCases(ByVal Xl As Object, ByVal Runs As Long) As String
    Dim Book As Object, Source As Object, Target As Object, Result As String
    QueryPerformanceFrequency Freq
    Xl.ScreenUpdating = False
    Xl.EnableEvents = False
    Set Book = Xl.Workbooks.Add
    Xl.Calculation = -4135
    Set Source = Book.Worksheets(1)
    Set Target = Book.Worksheets.Add
    Fill Source, 100000, 10
    Result = Result & Block(Source, Target, 1000, 10, Runs)
    Result = Result & CellByCell(Source, Target, 1000, 10, Runs)
    Result = Result & Block(Source, Target, 100000, 10, Runs)
    Book.Close False
    RunExcelCases = Result
End Function

Declarations

The module declares the counter its times are read from, in microseconds:

Private Declare PtrSafe Function QueryPerformanceCounter Lib "kernel32" (ByRef c As Currency) As Long
Private Declare PtrSafe Function QueryPerformanceFrequency Lib "kernel32" (ByRef f As Currency) As Long

Private Freq As Currency
Private Function Since(ByVal T0 As Currency) As Long
    Dim T1 As Currency
    QueryPerformanceCounter T1
    Since = CLng((T1 - T0) / Freq * 1000000#)
End Function

The source sheet holds numbers, and a blank cell where the sum of the row and the column is a multiple of 7:

'  numbers, and a blank where (row + column) Mod 7 = 0.
Private Sub Fill(ByVal Sheet As Object, ByVal Rows As Long, ByVal Cols As Long)
    Dim Data() As Variant, R As Long, C As Long
    ReDim Data(1 To Rows, 1 To Cols)
    For R = 1 To Rows
        For C = 1 To Cols
            If (R + C) Mod 7 <> 0 Then Data(R, C) = R * Cols + C
        Next
    Next
    Sheet.Range(Sheet.Cells(1, 1), Sheet.Cells(Rows, Cols)).Value = Data
End Sub

Cases

Block

The cells read into a Variant with one call (read), every element that is not Empty doubled with no call to Excel (pass), and the array written to the other sheet with one call (write). total is the three together. The case runs on 1,000 × 10 cells, then on 100,000 × 10.

Phase 1,000 × 10 100,000 × 10
Read Excel.Block1000x10.Read Excel.Block100000x10.Read
Pass Excel.Block1000x10.Pass Excel.Block100000x10.Pass
Write Excel.Block1000x10.Write Excel.Block100000x10.Write
Total Excel.Block1000x10.Total Excel.Block100000x10.Total
Private Function Block(ByVal Source As Object, ByVal Target As Object, ByVal Rows As Long, ByVal Cols As Long, ByVal Runs As Long) As String
    Dim V As Variant, R As Long, C As Long, Run As Long, T0 As Currency, ReadUs As String, PassUs As String, WriteUs As String, TotalUs As String
    Dim Start As Currency, Label As String
    For Run = 1 To Runs
        Target.Cells.ClearContents
        QueryPerformanceCounter Start
        QueryPerformanceCounter T0
        V = Source.Range(Source.Cells(1, 1), Source.Cells(Rows, Cols)).Value
        ReadUs = ReadUs & CStr(Since(T0)) & ";"
        QueryPerformanceCounter T0
        For R = 1 To Rows
            For C = 1 To Cols
                If Not IsEmpty(V(R, C)) Then V(R, C) = V(R, C) * 2
            Next
        Next
        PassUs = PassUs & CStr(Since(T0)) & ";"
        QueryPerformanceCounter T0
        Target.Range(Target.Cells(1, 1), Target.Cells(Rows, Cols)).Value = V
        WriteUs = WriteUs & CStr(Since(T0)) & ";"
        TotalUs = TotalUs & CStr(Since(Start)) & ";"
    Next
    Label = "Excel.Block" & Rows & "x" & Cols
    Block = Label & "|read|" & ReadUs & vbLf & Label & "|pass|" & PassUs & vbLf & Label & "|write|" & WriteUs & vbLf & Label & "|total|" & TotalUs & vbLf
End Function

CellByCell

The same work on 1,000 × 10 cells, one cell at a time: a call to Excel to read each cell and another to write it, 20,000 calls a run.

Excel.CellByCell1000x10.Total

Private Function CellByCell(ByVal Source As Object, ByVal Target As Object, ByVal Rows As Long, ByVal Cols As Long, ByVal Runs As Long) As String
    Dim X As Variant, R As Long, C As Long, Run As Long, Start As Currency, TotalUs As String
    For Run = 1 To Runs
        Target.Cells.ClearContents
        QueryPerformanceCounter Start
        For R = 1 To Rows
            For C = 1 To Cols
                X = Source.Cells(R, C).Value
                If Not IsEmpty(X) Then X = X * 2
                Target.Cells(R, C).Value = X
            Next
        Next
        TotalUs = TotalUs & CStr(Since(Start)) & ";"
    Next
    CellByCell = "Excel.CellByCell" & Rows & "x" & Cols & "|total|" & TotalUs & vbLf
End Function

See also


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca