Benchmarks: calls
Ce document est disponible en français
Calls to a procedure of the same module, to the members of an object, and to the standard library. Each case but BenchRecursion makes its calls in a loop that runs 100,000 times. The class modules the cases call are at the end of this page.
The source is the module Calls.bas of the corpus built into rdc. Each badge gives RD-VBA's time as a multiple of MS-VBA's (how the figures are measured).
Cases
BenchRecursion
Recursion: Fib(25), 242,785 short calls.
Private Function Fib(ByVal N As Long) As Long
If N < 2 Then
Fib = N
Else
Fib = Fib(N - 1) + Fib(N - 2)
End If
End Function
' recursion: Fib(25) is 242,785 calls, each of which is short, so the case measures what an activation costs.
Public Sub BenchRecursion()
Dim S As Long
S = Fib(25)
End Sub
BenchProcedureCalls
A function of the same module, called in a loop.
Add, the function it calls:
Private Function Add(ByVal A As Long, ByVal B As Long) As Long
Add = A + B
End Function
Public Sub BenchProcedureCalls()
Dim I As Long
Dim S As Long
For I = 1 To 100000
S = Add(I, 1)
Next
End Sub
BenchByRefCalls
A variable passed ByRef and assigned through the parameter.
Private Sub Increment(ByRef N As Long)
N = N + 1
End Sub
' a variable of the caller passed by reference, and assigned through the parameter.
Public Sub BenchByRefCalls()
Dim I As Long
Dim S As Long
For I = 1 To 100000
Increment S
Next
End Sub
BenchClassMemberCalls
A member of a class, called through a variable of the class.
It calls Counter.
Public Sub BenchClassMemberCalls()
Dim C As Counter
Set C = New Counter
Dim I As Long
For I = 1 To 100000
C.Increment
Next
Dim Total As Long
Total = C.Value
End Sub
BenchInterfaceMemberCalls
The same member, called through an interface.
It calls InterfaceCounter through ICounter.
Public Sub BenchInterfaceMemberCalls()
Dim C As ICounter
Set C = New InterfaceCounter
Dim I As Long
For I = 1 To 100000
C.Increment
Next
Dim Total As Long
Total = C.Value
End Sub
BenchStandardLibraryCalls
N = Len(CStr(I)) + CLng(I) in a loop.
Public Sub BenchStandardLibraryCalls()
Dim I As Long
Dim N As Long
For I = 1 To 100000
N = Len(CStr(I)) + CLng(I)
Next
End Sub
BenchFactoryCalls
A factory method of a default instance, a new object on every call.
It calls Gadget.
' a factory method off a class's default instance: a new object on every call, given its state by the factory.
Public Sub BenchFactoryCalls()
Dim G As Gadget
Dim I As Long
For I = 1 To 100000
Set G = Gadget.Create(I)
Next
End Sub
BenchDefaultInstanceCalls
A member called on a default instance.
It calls Gadget.
' a member called on the default instance itself.
Public Sub BenchDefaultInstanceCalls()
Dim I As Long
Dim S As Long
For I = 1 To 100000
S = Gadget.Twice(I)
Next
End Sub
BenchSiblingCalls
A member that calls a private member of its own class by its bare name.
It calls Gadget.
' a member that calls a private member of its own class by its bare name, which is a call on the current instance.
Public Sub BenchSiblingCalls()
Dim G As Gadget
Set G = Gadget.Create(3)
Dim I As Long
Dim S As Long
For I = 1 To 100000
S = G.Scaled(2)
Next
End Sub
Class modules
Counter
A class with a field, a method and a property.
Option Explicit
Private mValue As Long
Public Sub Increment()
mValue = mValue + 1
End Sub
Public Property Get Value() As Long
Value = mValue
End Property
ICounter
An interface: InterfaceCounter implements it.
Option Explicit
' An interface: what Calls.BenchInterfaceMemberCalls calls InterfaceCounter's members through.
Public Sub Increment()
End Sub
Public Property Get Value() As Long
End Property
InterfaceCounter
Counter, written as the implementation of ICounter.
Option Explicit
Implements ICounter
Private mValue As Long
Private Sub ICounter_Increment()
mValue = mValue + 1
End Sub
Private Property Get ICounter_Value() As Long
ICounter_Value = mValue
End Property
Gadget
A class with a default instance (Attribute VB_PredeclaredId = True) and a factory method.
Option Explicit
' A factory method off a stateless default instance, the pattern object-oriented VBA is built on: the object it makes is given its state through a Friend
' setter that only the factory uses, and a member guards against being called on the default instance by calling a private member of the class.
Private Type TGadget
Size As Long
End Type
Private This As TGadget
Public Function Create(ByVal Size As Long) As Gadget
Dim Result As Gadget
Set Result = New Gadget
Result.Size = Size
Set Create = Result
End Function
Public Property Get Size() As Long
Size = This.Size
End Property
Friend Property Let Size(ByVal Value As Long)
This.Size = Value
End Property
Public Function Scaled(ByVal Factor As Long) As Long
GuardInstance
Scaled = This.Size * Factor
End Function
Public Function Twice(ByVal N As Long) As Long
Twice = 2 * N
End Function
Private Sub GuardInstance()
If Me Is Gadget Then Err.Raise 5
End Sub
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca