Use ByRef in VB6 to increase the speed

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.