简体   繁体   中英

What are some Performance [Dos/Don'ts] in C# -ASP.NET

I am finalizing one of my projects and taking a look over the whole project looking for mistakes, bugs and performance mistakes. I am using MVC. I caught one Don't and that is:

Never put a RenderPartial within a loop. it will drastically slow down your whole server.

Never store a WebControl to Session.

Because it has a reference to the Page object, it ends up storing every control to session.

Don't optimize prematurely. :) If the site is not performant, profile the code to determine where to spend your time.

Have you run your program through FxCop? It has a set of rules for performance.

Don't profile or otherwise judge performance in the debug configuration. The debug configuration isn't intended to be fast, and you may make performance conclusions which are wrong (like the idea that partial views/user controls are slow; this is true in debug configuration but not in release configuration). When you profile to measure performance, you should use the release configuration so that you can see where the real problems are.

不要乱用显式垃圾收集。

Most performance problems are due to disk access or calls across networks.

So be carefull how and how often you access the file system or a database. Do you need to make so many calls across the network, or could you do it in a single call.

One good example:

  • value is stored in session
  • session is configured to use SQL server
  • value is used only once every ten requests
  • for each request the value will be read from the database then written to the database

In this case a better solution may be to write custom code to store and read the value.

缓存可以帮助您提高性能,但是只有在有意义的地方才应该小心使用它

DO use static methods - but only if the method is frequently used.

DON'T mark a variable as static unless you really want the variable's value to be the same across all instances (another developer did this and I had fun debugging why we got odd behavior only when multiple users hit the site). This is not for performance reasons, but just good advice.

In C#, objects are always created with new. This alone can be a drawback in a certain perspective. For example, if you create an object inside a loop (meaning that a new object is created for each iteration in the loop) you can slow down your program.

for (int i = 0; i < 1000; ++i)
{
   Object o = new Object();
   //...
}

Instead create an instance outside the loop. Object o = new Object();

Object o = new Object();
for (int i = 0; i < 1000; ++i)
{
   //...
}

Only create an object in a loop if you really have to...

Perhaps doing a bit of C++ would help you understand the mechanics behind and know when and where to optimize your code. Although C++ is a different language there are a lot of things you can apply to other languages once you understand the basic of memory management (new, delete, pointers, dynamic arrays/static arrays, etc.).

Only use try/catch blocks when necessary. They do slow down your application.

EDIT for Clarity: By "necessary" I mean, for catching real errors.

If you can write some code and be proactive to ensure the error won't be thrown do it, as it will be more performant than letting an exception be thrown, then handling it.

Don't use exceptions to control program flow. I don't know who said it first, but I recall the phrase " Exceptions should be exceptional! ". They should be for the cases where unforseen issues occur, things that couldn't be tested prior to code executing and throwing them.

The worst example I see all to often is something along these lines...

int i = 0;
try
{
    i = int.Parse(txt);
} catch {Exception x) {
    // Do nothing, i = 0
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM