For a long time in VB6, parameters are transmitted by ByRef by default, that is, transmitted by address)
In. Net (C #, VB. Net), parameters are transmitted by ByVal (that is, by value) by default.
During the optimization program these days, it is found that the default method (ByVal) is quite inefficient when large variables are transferred.
For example, the input parameter variable types are large strings, arrays, sets, DataSet, etc.
The key code for testing is as follows. The input string is not very large. The larger the variable, the higher the ByRef efficiency. Of course, when the input variable can be modified or has no other function, you can use ByRef to transfer data.
Copy codeThe Code is as follows:
Private Declare Function GetTickCount Lib "kernel32" () As Int32
Private Function TestByRef (ByRef aa As String) As String
Aa = "1" & aa
TestByRef = aa
End Function
Private Function TestByVal (ByVal aa As String) As String
Aa = "1" & aa
TestByVal = aa
End Function
Private Sub button#click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1.Click
Dim teststr As String
Dim Newstr As String
Dim t As Int32
Dim II As Int32
Teststr = "wosdfsdfdsfdsfsfdsfsfsfsfsfsfdsfdsfcvxcvxcvcxvvcxvvvxvcvxv"
T = GetTickCount
For II = 1 To 10000
Newstr = TestByRef (teststr)
Next
MsgBox ("ByRef" & CStr (GetTickCount-t ))
T = GetTickCount
For II = 1 To 10000
Newstr = TestByVal (teststr)
Next
MsgBox ("ByVal" & CStr (GetTickCount-t ))
End Sub