Error | Error handling | speed
In VB6, we use on error to catch and handle errors, and we often use on error Resume Next to ignore errors that may arise.
So does using error trapping affect speed?
Yes, using error handling will degrade some of the performance in. Net, using Try Catch will also affect performance, so we use
When Reflector decompile Microsoft's class library, it finds that his child processes rarely use Try to catch errors, basically by predicting possible errors in a predictable manner and handling them accordingly.
In fact, after using error trapping, the compiled code actually does a lot of things that are not known to us, although using error trapping to some extent facilitates the coding process, sacrificing a certain speed and pros and cons, so we should use error trapping correctly.
VB6 Code for testing
Option Explicit
Dim cn as New ADODB. Connection
Dim rs as New ADODB. Recordset
Dim M_fldsname () as String
Private Sub Command1_Click ()
Dim II as Long
Dim T as single
cn. Open "Provider=SQLOLEDB.1; Persist Security Info=false; User id=sa;initial catalog= Guang shang; Data source=super "
Rs. Open "Select * from Fldset", CN, adOpenKeyset
Do Until Rs. EOF = True
Rs. MoveNext
Loop
Rs. MoveFirst
t = Timer
ReDim M_fldsname (Rs. Fields.Count)
For II = 0 to Rs. Fields.count-1
M_fldsname (II) = Rs. Fields (II). Name
Next
Do Until Rs. EOF = True
Readfieldsnotonerr "ERR1"
Readfieldsnotonerr "TableName"
Readfieldsnotonerr "ERR2"
Rs. MoveNext
Loop
MsgBox "Noterr:" & timer-t
On Error Resume Next
Rs. MoveFirst
t = Timer
Do Until Rs. EOF = True
Readfieldsonerr "ERR1"
Readfieldsonerr "TableName"
Readfieldsonerr "ERR2"
Rs. MoveNext
Loop
MsgBox "Onerr:" & timer-t
Rs. Close
cn. Close
End Sub
Private Sub Readfieldsonerr (FieldName as String)
Dim V as Variant
v = RS (FieldName). Value
End Sub
Private Sub Readfieldsnotonerr (FieldName as String)
Dim II as Long
Dim Isexists as Boolean
Dim V as Variant
Isexists = False
For II = 0 to UBound (m_fldsname)-1
If m_fldsname (II) = FieldName Then
Isexists = True
Exit for
End If
Next
If isexists = True Then
v = RS (FieldName). Value
End If
End Sub
===================================
Test results:
When there is no error
readfieldsonerr:0. 46
readfieldsnotonerr:0. 47
When there is a mistake
readfieldsonerr:0.96
readfieldsnotonerr:0.47
You can see: without error handling, the speed is fairly stable, while using error handling, there are errors in the case of a greater speed gap
So for subroutines that are called frequently in loops, it is recommended that possible errors be preprocessed to reduce the use of On error
To increase speed.