Different from. Net, in VB6, ByRef is used by default to pass parameters. It seems that there is nothing to optimize.
However, if you call an API, the Code copied from the API browser is forced to use ByVal to pass the parameter.
This gives us the opportunity to optimize.
I used this code again to test the difference between ByRef and ByVal. The result is very exciting: ByRef is 16 times higher than ByVal.
The Code is as follows. The longer the string inserted into the ComboBox, the more powerful the ByRef command is.
Option Explicit
Private Declare Function SendMessageByRef Lib "user32" Alias "SendMessageA" (ByRef hWnd As Long, ByRef wMsg As Long, ByRef wParam As Long, ByRef lParam As Any) As Long
Private Declare Function SendMessageByVal Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const CB_ADDSTRING = & H143
Private Sub commandementclick ()
Dim II As Long
Dim t As Long
Dim s As String
Combo1.Clear
T = GetTickCount ()
Use API ByRef
For II = 1 To 10000
S = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Assign a value again to change the address
SendMessageByRef Combo1.hWnd, CB_ADDSTRING, 0, s
Next
MsgBox "ByRef" & GetTickCount-t
Combo1.Clear
T = GetTickCount ()
Use API ByVal
For II = 1 To 10000
S = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
SendMessageByVal Combo1.hWnd, CB_ADDSTRING, 0, s
Next
MsgBox "ByVal" & GetTickCount-t
Combo1.Clear
T = GetTickCount ()
Use common ComboBox Add
For II = 1 To 10000
S = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Combo1.AddItem s
Next
MsgBox "ComboBox Add" & GetTickCount-t
End Sub
Changing the parameter passing method of the API to ByRef can increase the speed. Of course, not all API parameters can be modified in this way. It depends on whether the passed parameters are modified in the API function, or whether you want to continue using the passed parameters.
Otherwise, some errors may occur, and some APIs do not allow passing Parameters Using ByRef. The specific situation must be analyzed. Of course, the above column can use Byref