How to write high quality VB code
Introduction:
This article describes how to improve the performance efficiency of VB code through some technical means. These means can be divided into two large parts: coding technology and compilation optimization techniques. In the encoding technique, how to improve the code execution speed and reduce the system resources of code consumption by reducing external references such as external reference. In the compilation optimization technology, the compilation options that correctly use VB are optimized to optimize the executable generated by the last generated time generated.
Foreword
What is an efficient software? A efficient software should not only run faster than the software that achieve the same function, but also consume fewer system resources. This article brings together some of the authors who have accumulated in software development when using VB, through some simple examples to show you how to write efficient VB code. It contains some techniques that may be very helpful to VB programmers. Before starting, let me first let me know a few concepts.
Let code once: among the programmers I have touched, there are many people like to write the code according to the functional needs, and then optimize the code on this basis. Finally, we have to re-write the code again in order to achieve optimization. So I suggest that you need to consider optimization before writing code.
Grasp the results of optimization and the relationship between work needs to be taken: Usually, you need to check and modify it. In the process of checking the code, maybe you will find that the code efficiency in some cycles can also be further improved. In this case, many of the pursuit of perfect programmers may immediately modify the code. My suggestion is that if this code will make the program's runtime shorten, you can modify it. If you can only bring 10 millisecond performance improvements, don't make any changes. This is because rewriting a code will definitely introduce new errors, and debugging new code will definitely spend a certain time. Programmers should find a balance between software performance and development software, and 10 milliseconds are also a difference that cannot be realized for users.
Try to use it when you need to use object-oriented methods; VB provides the mechanism that does not fully support object-oriented design and encoding, but VB provides a simple class. Most people think that the use of objects will result in low efficiency of the code. For this point I personally have some different opinions; the efficiency of the exam code cannot be purely from the perspective of running speed, the resources occupied by the software also need to consider the factors. Use classes to help you improve your software as a whole, which will be described in detail in later examples.
When you write VB code, I hope you can use the above point as the principle of guiding your coding. I divide the article into two parts: how to improve the running speed and compilation optimization of the code.
How to improve the running speed of the code
The following methods can help you improve your code's running speed:
1. Using an integer and long integer (long)
Improve the easiest way to run the code run is too used to use the correct data type. Maybe you don't believe, but correctly select the data type can greatly enhance the performance of the code. In most cases, the programmer can replace the variable of the SINGLE, DOUBLE and CURRENCY types with the Integer or LONG type variable because VB processes Integer and Long far more than processing other data types.
In most cases, programmers choose to use Single or Double because they can save decimals. However, decimal can also be saved in the Integer type variable. For example, there are about three decimals in the program, then only the values saved in the Integer variable can be obtained in 1000. According to my experience, after using Integer and long, after SINGLE, DOUBLE and CURRENCY, the running speed of the code can be increased by nearly 10 times.
2. Avoid using variants
For a VB programmer, this is a matter of obvious things. Variable type variables require 16 bytes of space to save data, and an integer requires only 2 bytes. Usually the purpose of the variant type is to reduce the amount of design and code, and some programmers use it. But if a software has been strictly designed and coded in accordance with the specification, it can avoid the use of variant types. Here, there is also the same problem for the Object object. Please see the following code:
DIM FSO
Set fso = new scripting.filesystemObject
or
DIM FSO As Object
Set fso = new scripting.filesystemObject
The above code is wasting the memory and CPU time when the value is not specified during the declaration. The correct code should be like this:
DIM FSO AS New FileSystemObject
3. Try to avoid the use of properties
In the usual code, the most common relatively inefficient code is to use the attribute (Property), especially in the cycle. To know that the speed of access variable is about 20 times the speed of access attribute. The following code is that many programmers will use in the program:
DIM INTCON AS INTEGER
For intcon = 0 to Ubound (somvar ())
Text1.text = text1.text & vbcrf & somevar (intcon)
Next Intcon
The execution speed of the code below is 20 times the above code.
DIM INTCON AS INTEGER
DIM SOUTPUT AS STRING
For intcon = 0 to Ubound (Somevar ())
SOUTPUT = SOUTPUT & VBCRLF &
Somevar (intcon)
NEXT
TEXT1.TEXT = SOUTPUT
4. Try to use arrays to avoid using collection
Unless you have to use a collection, you should try to use an array. According to the test, the access speed of the array can reach 100 times the collection. This figure sounds a bit awkward, but if you consider the collection is an object, you will understand why the difference will be so big.
5. Expand small cyclic body
At the time of encoding, it is possible to encounter this situation: one cycle will only circulate 2 to 3 times, and the cyclic body consists of several lines of code. In this case, you can expand the loop. The reason is that the loop will take up additional CPU time. But if the loop is more complicated, you don't have to do this.
6. Avoid using a short function
Similarly to using small cycles, calling only a few lines of code is also an economical - the time spent in calling functions may take longer than the code in the execution function. In this case, you can copy the code in the function to the place where the function is called.
7. Reduce the reference to child objects
In VB, by use. Reference to the object. E.g:
Form1.text1.text
In the above example, the program references two objects: Form1 and Text1. This method is low. But unfortunately, there is no way to avoid it. The only programmer can do it is useless or will hold the sub-object with another object.
Note: Using with
With frmmain.text1
.Text = "Learn VB"
.Alignment = 0
.Tag = "ITS My Life"
.BACKCOLOR = VBLACK.FORECOLOR = VBWHITE
End with
or
Note: Use another object to save sub-objects
DIM TXTTEXTBOX As TextBox
Set txttextbox = frmmain.text1
TXTTEXTBOX.TEXT = "Learn VB"
TXTTEXTBOX.ALIGNMENT = 0
TXTTEXTBOX.TAG = "ITS My Life"
TXTTEXTBOX.BACKCOLOR = VBBLACK
TXTTextBox.ForeColor = VBWHITE
Note that the method mentioned above is only applicable to the time when the child objects need to be operated, the following code is incorrect:
WITH TEXT1
.Text = "Learn VB"
.Alignment = 0
.Tag = "ITS My Life"
. Backcolor = vbbble
. Manufactol = vbwhite
End with
Unfortunately, we can often find similar to the above code in the actual code. This will only make the code's execution speed slower. The reason is that the WITH block is compiled and a branch will increase the additional processing.
8. Check if the string is empty
Most programmers use the following methods when checking whether the string is empty:
If Text1.Text = "" ""
Note: Perform operation
END IF
Unfortunately, the amount of handles required for strings is even more than reading attributes. So I suggest that you use the following methods:
If Len (Text1.Text) = 0 THEN
Note: Perform operation
END IF
9. Remove the variable name after the next keyword
After the NEXT keyword, add the variable name caused the efficiency of the code to decrease. I don't know why, just an experience. However, I think very few programmers will draw such a snake, after all, most of the programmers are all people who are like gold.
Note: Error code
For iCount = 1 to 10
Note: Perform operation
Next iCount
Note: Correct code
For iCount = 1 to 10
Note: Perform operation
NEXT
10. Use arrays instead of multiple variables
When you have multiple variables that save similar data, you can consider replacing them with one array. In VB, arrays are one of the most efficient data structures.
11. Use dynamic arrays instead of static arrays
Using the dynamic array's execution speed of the code does not have too much impact, but in some cases a large amount of resources can be saved.