Bancs d'essai : appels
This document is available in English
Des appels à une procédure du même module, aux membres d'un objet et à la bibliothèque standard. Chaque cas sauf BenchRecursion fait ses appels dans une boucle de 100 000 tours. Les modules de classe qu'appellent les cas sont à la fin de cette page.
La source est le module Calls.bas du corpus intégré à rdc. Chaque badge donne le temps de RD-VBA en multiple de celui de MS-VBA (comment les chiffres sont mesurés).
Cas
BenchRecursion
La récursion : Fib(25), 242 785 appels courts.
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
Une fonction du même module, appelée dans une boucle.
Add, la fonction qu'il appelle :
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
Une variable passée ByRef et affectée par le paramètre.
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
Un membre d'une classe, appelé par une variable de la classe.
Il appelle 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
Le même membre, appelé par une interface.
Il appelle InterfaceCounter par 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) dans une boucle.
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
Une méthode de fabrique d'une instance par défaut, un nouvel objet à chaque appel.
Il appelle 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
Un membre appelé sur une instance par défaut.
Il appelle 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
Un membre qui appelle un membre privé de sa propre classe par son seul nom.
Il appelle 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
Modules de classe
Counter
Une classe à un champ, une méthode et une propriété.
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
Une interface : InterfaceCounter l'implémente.
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, écrit comme l'implémentation de 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
Une classe à instance par défaut (Attribute VB_PredeclaredId = True) et à méthode de fabrique.
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
Voir aussi
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca