首页 > 编程 > .NET > 正文

asp.net优化(二)

2024-07-10 12:58:11
字体:
来源:转载
供稿:网友
    1. do not rely on exceptions in your code: exceptions are very expensive and should rarely occur in your code. you should never use exceptions as a way to control normal program flow. if it is possible to detect in code a condition that would cause an exception, you should do that instead of waiting to catch the exception before handling that condition. common scenarios include checking for null, assigning to a string that will be parsed into a numeric value, or checking for specific values before applying math operations. for example:

    1. // consider changing this:try {   result = 100 / num;}catch (exception e) {  result = 0;}// to this:if (num != 0)   result = 100 / num;else  result = 0;
    ' consider changing this:try   result = 100 / numcatch (e as exception)  result = 0end try// to this:if not (num = 0)   result = 100 / numelse  result = 0end if
    // consider changing this:try {   result = 100 / num;}catch (e:exception) {  result = 0;}// to this:if (num != 0)   result = 100 / num;else  result = 0;
    c# vb jscript
    1. use early binding in visual basic or jscript code: one of the advantages of visual basic, vbscript, and jscript is their typeless nature. variables can be created simply by using them and need no explicit type declaration. when assigning from one type to another, conversions are performed automatically, as well. this can be both an advantage and a disadvantage, since late binding is a very expensive convenience in terms of performance.
    2. the visual basic language now supports type-safe programming through the use of a special option strict compiler directive. for backward compatibility, asp.net does not enable option strict by default. however, for optimal perfomance, you should enable option strict for your pages by using a strict attribute on the page or control directive:
    3. <%@ page language="vb" strict="true" %><%dim bdim c as string' this causes a compiler error:a = "hello"' this causes a compiler error:b = "world"' this does not:c = "!!!!!!"' but this does:c = 0%>
    4. jscript also supports typeless programming, though it offers no compiler directive to force early binding. a variable is late-bound if:
    5. it is declared explicitly as an object.
    • it is a field of a class with no type declaration.
    • it is a private function/method member with no explicit type declaration and the type cannot be inferred from its use.
    1. the last distinction is complicated. the jscript compiler optimizes if it can figure out the type, based on how a variable is used. in the following example, the variable a is early-bound but the variable b is late-bound:
    2. var a;var b;a = "hello";b = "world";b = 0;
    3. for the best performance, declare your jscript variables as having a type. for example, "var a : string".
    4. port call-intensive com components to managed code: the .net framework provides a remarkably easy way to interoperate with traditional com components. the benefit is that you can take advantage of the new platform while preserving your existing code. however, there are some circumstances in which the performance cost of keeping your old components is greater than the cost to migrate your components to managed code. every situation is unique, and the best way to decide what needs to be changed is to measure site performance. in general, however, the performance impact of com interoperability is proportional to the number of function calls made or the amount of data marshaled from unmanaged to managed code. a component that requires a high volume of calls to interact with it is called "chatty," due to the number of communications between layers. you should consider porting such components to fully managed code to benefit from the performance gains provided by the .net platform. alternatively, you might consider redesigning your component to require fewer calls or to marshal more data at once.
    5. use sql stored procedures for data access: of all the data access methods provided by the .net framework, sql-based data access is the best choice for building scalable web applications with the best performance. when using the managed sql provider, you can get an additional performance boost by using compiled stored procedures instead of ad hoc queries. for an example of using sql stored procedures, refer to the server-side data access section of this tutorial.
    6. use sqldatareader for a fast-forward, read-only data cursor: a sqldatareader object provides a forward, read-only cursor over data retrieved from a sql database. because sqldatareader uses tabular data stream (tds) packets to read data directly from a database connection, it provides better performance than using a dataset, if you can use that for your scenario. because sqldatareader supports the ienumerable interface, you can even bind server controls, as well. for an example of using sqldatareader, see the server-side data access section of this tutorial.
    7. cache data and output wherever possible: the asp.net programming model provides a simple mechanism for caching page output or data when it does not need to be dynamically computed for every request. you can design your pages with caching in mind to optimize those places in your application that you expect to have the most traffic. more than any feature of the .net framework, the appropriate use of caching can enhance the performance of your site, sometimes by an order of magnitude or more. for more information about how to use caching, see the cache services section of this tutorial.
    8. enable web gardening for multiprocessor computers: the asp.net process model helps enable scalability on multiprocessor machines by distributing the work to several processes, one for each cpu, each with processor affinity set to its cpu. the technique is called web gardening, and can dramatically improve the performance of some applications. to learn how to enable web gardening, refer to the using the process model section.
    9. do not forget to disable debug mode: the <compilation> section in asp.net configuration controls whether an application is compiled in debug mode, or not. debug mode degrades performance significantly. always remember to disable debug mode before you deploy a production application or measure performance. for more information about debug mode, refer to the section entitled the sdk debugger.
    中国最大的web开发资源网站及技术社区,
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表