Impact of error handling on speed in VB6

Source: Internet
Author: User
Tags try catch

In VB6, we use on error to capture and handle errors, and often use on error resume next to generate possible ignore errors.

Will error capture affect the speed?

Yes, using error handling will reduce some performance. . Net, using try catch will also affect the performance, so we use

When reflector decompile Microsoft's class library, it will find that its sub-processes seldom use try to capture errors. Generally, they use predictable methods to determine possible errors and handle them accordingly.

In fact, after error capture is used, the compiled code actually does a lot of operations that we don't know about. Although it facilitates the encoding process to a certain extent, but it sacrifices a certain speed, which has advantages and disadvantages. Therefore, we should use error capture correctly.

Tested VB6 code

Option explicit

Dim cn as new ADODB. Connection
Dim RS as new ADODB. recordset
Dim m_fldsname () as string

Private sub commandementclick ()

Dim II as long
Dim t as single

CN. Open "provider = sqloledb.1; persist Security info = false; user id = sa; initial catalog = guangshang; 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
End if
Next

If isexists = true then
V = RS (fieldname). Value
End if
End sub

==========================================

Test results:

No error

Readfieldsonerr: 0. 46

Readfieldsnotonerr: 0. 47

Incorrect

Readfieldsonerr: 0.96

Readfieldsnotonerr: 0.47

We can see that error handling is not used, and the speed is quite stable. When error handling is used, the speed gap is large when errors exist.

Therefore, we recommend that you pre-process possible errors in subprocesses that are frequently called in loops to reduce the use of on error.

To speed up

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.