Minor changes improve program performance

Source: Internet
Author: User
Compared with the original For Statement Foreach With better execution efficiency, Foreach The average time spent is only For Of 30% . The test result is displayed in For And Foreach We recommend more efficient Foreach . In addition, Use For The write time is about the read time. 10 Times or so;

Ø InProgram, UseSAAn account is very insecure and has the permission to perform any operations on the database. A hacker can call something similarXp_mongoshellSuch extended stored procedures are attacked;

ØWhen processing data interactions, try to organizeSQLStatement processing is placed in the middle layer (such as specialized data operations and stored procedures), which is an effective way to prevent injection attacks;

Ø Avoid unnecessary round-trip to the server. UsePage. ispostbackAvoid unnecessary handling of the round-trip process;

ØMake sure to disable the debugging mode;

ø generally, , reads large amounts of data , the returned data is not processed in large quantities. sqldatareader. datset more suitable . for sqldatareader and dataset depends on the implementation of program functions;

ØRecommended Data Binding<% # Ctype (container. dataitem, datarowview). Row ("Field name") %>.When the data volume is large, the speed can be increased by several hundred times;

Ø Use " Short Circuit " Logical operation
In VB.net Used in logical operations Andalso Replace And , Orelse Replace Or ,
In C # Used in logical operations && Replace & , Use | Replace |.
The so-called " Short Circuit " Operation refers -CompiledCodeYou can skip the calculation of another expression based on the calculation result of one expression. For example:
Logical operation If a and B then , Where A And B Yes. When the code is run, it must be calculated separately. A And B And if it is
If a andalso B then , A Calculate first, if A Is True . B . In this case, if B Complex computing or
Process call involved, use Andalso This will reduce unnecessary performance loss.
For Or And Orelse .

ø use + = , -= simplified operators
for example, original expression A = a + B
A only appears once, and the system performance is improved during runtime
The following operators are available: * = , " = , /= , & = , etc.

ø use stringbuilder replaces string
string types are unchangeable in the memory, any modifications made to the defined string type variables are actually performed on
A new string , and the sringbuilder class is to create an object in the memory, all the operations on these objects are modifications to the
object. Therefore, use stringbuilder instead of string improves the runtime performance.

ø define variable within the cycle
example: for (INT I = 1; I <= 10; I ++)
{< br> ....
}< br> instead of: int I;
for (I = 1; I <= 10; I ++)
{< br> ....
}

ØSetOption strictEnable
InVs.netEnableOption strict, Strict conversion of data types in the code is required to avoid performance impact caused by System Conversion

Ø In VB.net Use for type conversion Directcast Replace Ctype
In VB.netIf you convert a type of object to its inheritance type object, for example "Control" Convert to one Textbox Hour (Textbox Slave Control Inheritance ) Please Use Directcast Replace Ctype This will improve the system performance, but if you want Integer Convert String , Cannot Because Integer And StringThe type is not an inheritance relationship.
In C # No and Directcast Corresponding function.

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.