Here are some things I have done to speed up my pages (things I have picked up over time):
- Set the Option Strict and Option Explicit to On in the Project Properties. Option Explicit requires you to declare your variables and objects before using them and is set to “On” by default. Option Strict requires you to explicitly declare datatype conversions by disallowing late binding. This takes the guesswork out during run time, thus faster execution.
- Set Session State to ReadOnly where you can. Read only maintains state, but may bounce the session when moving to a page where session state is set to true. There is also False, which is a good idea if your page is going to be completely public and doesn't need to hold any session information at all.
- Set ViewState to False (makes a huge difference) where you can. If a control on the page needs session state, I leave it on for the page, but turn it off on all of the server controls that do not need it (yes, you can do this).
- Cache items where you can. This not only include pages, but also information, such as datasets.
- Use System.Text.StringBuilder instead of Strings (because strings are immutable). If you make more than 3 changes to a string (this includes concatenations), you should use the stringbuilder instead.
- Real life example: An application I built was pulling information from Active Directories and displaying it in a page that included contact information. If someone pulled the entire directory for display, it took between 50-80 seconds to display everything. I went in today and optimized it by using the string builder instead of strings. From that change alone I went from 50+ seconds to 2-3 seconds! Then I also started caching each of the individual OU's table information into the cache so that everything would check the cache first, and then pull from active directories if it wasn't there. That reduced the entire directory pull to 1 second. That is great considering I am pulling information about over 3,200 employees.
- Build and deploy the Release version. It is smaller and will execute faster.
- Use ngen to precompile your application dlls after deployment. Ngen is short for Native Image Generator. What it does is precompile as much of the code as it can for the processor of the machine that the application is on. This means less code to execute by the JIT (Just In Time) compiler, which means faster execution of pages.
- Set the debug attribute in the web.config file to False. In fact, take everything that you don't use out of the web.config file. If you don't need it for your application, remove it. My suggestion is to make a copy of it first, that way if you pull something out that you may have needed, you will have it right there to set it back. You can try to pull everything out, then put things in until the application works again (to the desired results).
Following some tips, I have taken ngen and have made it an automated batch that I just add directories to, and it will run a nightly check on those directories and dump the output into a log file that I have put with it. I can even have it email me, but I would rather not. Why would I do this, you would ask? Well, some people work in a situation where they only deploy once. We deploy constantly in the development stages. I can be on the phone with a user and have made changes and deployed them twice in five minutes. We try to make our applications as bug-free as we can, but that is not always the case in coding. When we pick up new tips, we refactor our code.