Skip to content

Measurement Method

Daniele Penza edited this page Dec 5, 2026 · 4 revisions

πŸ“ Measurement Method

Runs Host Bitness Cross-check

πŸ§ͺ How each figure was obtained

Every benchmark module in docs/benchmarks/ follows the same steps:

  1. Pre-flight check. Before timing, the module counts running EXCEL.EXE processes and GitHub runner jobs (Runner.Worker.exe) and samples CPU load for five seconds. It refuses to run if another Excel is open, a runner job is active, or the CPU averages over 10%.
  2. Same result first. Every variant of a group returns a checksum; a timing is reported only for variants that compute the same result.
  3. Warm-up, then five timed runs per variant, with the order reversed on every other run (A B C, C B A, ...) so a drift in load hits every variant.
  4. Median of the five runs, timed with QueryPerformanceCounter; the spread (slowest - fastest) / median flags noisy groups.
  5. Unedited output. The sheet each run writes is committed as is.
Run Scope Module
A Language basics M_BookBench.bas
B Numbers and dates M_BookBench2.bas
C Cross-check of run A with VBA Performance Manager M_BookBench_PM.bas
D Strings M_BookBench3.bas
E Arrays and collections M_BookBench4.bas
F Files, classes, binding M_BookBench5.bas, M_BookBench5_Early.bas, cBench5Item.cls, cBench5Node.cls
G Windows API on 64-bit Office M_BookBench6.bas
H CSV import and export, 10 MB UTF-8 file M_BookBench7.bas
I Working with the sheet: cells, formatting, Select, clipboard, row deletes, whole columns M_BookBench8.bas
J Lookups by table size, Evaluate, Calculate, volatile UDFs, state guards with formulas and events M_BookBench9.bas, cBench9Events.cls
K Growing arrays, Debug.Print and the status bar, IIf, loop conditions, objects created in a loop M_BookBench10.bas

🎯 How far to trust the numbers

  • βœ… Cross-checked. VBA Performance Manager reproduced run A within 4-8% in absolute time and within 2% in ratio for the three workloads of 14-700 ms.
  • ⚠️ Short workloads are unreliable through Application.Run. One dispatch costs about 24 us: a 0.026 ms workload measured 0.058 ms through it. Time blocks of at least about 10 ms, or subtract a baseline measured on an empty Sub.
  • ⚠️ One machine, except run K. Ratios travel well between machines; absolute times do not. Run K ran on two machines, both with the VBA editor open: a forced run on the usual one, while another Excel and a runner job were busy, and a clean run on a second one (Excel build 20527). Only the ratios above 2x that both runs agree on are quoted.
  • ⚠️ A clean workbook. Volatile cells anywhere in the workbook change the cost of every write: the first run J left 500 volatile UDF cells on a scratch sheet, and two of its groups had to be measured again.
  • πŸ§ͺ Figures marked estimate extrapolate a measured growth rate to a larger size.

⏱️ Clocks available to VBA

Clock Smallest step observed Cost of one read Use
Timer 3.9 ms (1/256 s); resets at midnight too coarse to measure ❌ benchmarks
timeGetTime 1 ms too coarse to measure ❌ short benchmarks
System tick (GetTickCount) 15.6 ms too coarse to measure ❌ benchmarks
QueryPerformanceCounter 0.1 us (10 MHz) about 1.7 us (an API call) βœ… benchmarks
#If VBA7 Then
    Private Declare PtrSafe Function QueryPerformanceCounter Lib "kernel32" (ByRef lpCount As Currency) As Long
    Private Declare PtrSafe Function QueryPerformanceFrequency Lib "kernel32" (ByRef lpFrequency As Currency) As Long
#Else
    Private Declare Function QueryPerformanceCounter Lib "kernel32" (ByRef lpCount As Currency) As Long
    Private Declare Function QueryPerformanceFrequency Lib "kernel32" (ByRef lpFrequency As Currency) As Long
#End If

Public Function SecondsFor(ByVal Repeats As Long) As Double
    Dim f As Currency, t0 As Currency, t1 As Currency, i As Long
    QueryPerformanceFrequency f
    QueryPerformanceCounter t0
    For i = 1 To Repeats
        ' code under test: repeat it until the block takes at least about 10 ms
    Next i
    QueryPerformanceCounter t1
    SecondsFor = (t1 - t0) / f          ' the Currency scale factor cancels out
End Function

Tip

Never time one short operation: a single clock read costs more than most VBA statements. Time a loop of many, and divide.

πŸ” Repeating a run

  1. Import the module from docs/benchmarks/ into an empty macro-enabled workbook (for run F, import the two .cls files first; for run J, cBench9Events.cls).
  2. Close every other Excel window and run its entry point: BENCH_RunAll, BENCH2_RunAll ... BENCH10_RunAll, or BENCH5E_RunAll (needs a reference to Microsoft Scripting Runtime).
  3. Save the sheet it writes as a new results file. Never edit an old one.

⚑ VBA Performance

Topics

Reference

Clone this wiki locally