Use of ByRef lifting speed in VB6

Source: Internet
Author: User
Speed

Unlike. Net, the default in VB6 is to pass arguments by using BYREF, and there seems to be nothing to optimize.

But, actually, if you invoke the API, the code copied from the API browser is forced to use ByVal.

This gives us a chance to optimize. Combined previously written

Use API SendMessage to improve the speed of inserting Vb.combobox Item

I again used the code to test a ByRef, ByVal the difference, the result is very exciting: ByRef is 16 times times higher than ByVal

The code is as follows: The longer the string inserted into the ComboBox, the more it can display the ByRef power.

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 Command1_Click ()

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 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
' Re-assign the value 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 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
Sendmessagebyval Combo1.hwnd, cb_addstring, 0, s

Next


MsgBox "ByVal" & Gettickcount-t

Combo1.clear
t = GetTickCount ()

' Use ordinary ComboBox Add
For II = 1 to 10000
s = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
Combo1.additem s

Next


MsgBox "ComboBox Add" & gettickcount-t

End Sub

The API will be changed to BYREF, you can improve speed, of course, not all of the API parameters can be so changed, specifically to see if the API function will be the incoming parameters to modify, or whether you will continue to use the parameters passed in.

This can cause some errors, and some APIs do not allow BYREF to be used. The specific situation has to be analyzed concretely. Of course, you can use Byref entirely.




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.