Tips And Tricks: Performance Tips and Techniques in .Net

Few Performance Tips in .Net

(will be updated regularly)

Following are few tips collated from different reading material found in Books and Internet.

1) Shortcut assignment operators

Using the shortcut assignment operators reduces the typing you need to do. They are also more efficient at runtime, because the variable needs to be evaluated only once.

commonly used shortcut assignment operators like these +=, -=, *=, /=, \=, &=,should be used.

For Eg. Use sum += value instead of sum= sum+ value. Since, this can result in lots of unnecessary work. This forces the JIT to evaluate both copies of sum, and many times this is not needed. The first statement can be optimized far better than the second, since the JIT can avoid evaluating the sum twice.

2) Exception Handling

The general rule of thumb is to put exception as close to the end user-generated event that caused the issue as possible, but still retain enough information to handle the exception as needed. For example, the structured error handler would be in the Click event procedure for the button(Submit/Save).

Another rule of thumb is to never catch an exception unless you plan to do something with it.You should not catch an exception and then simply rethrow it. That has a negative impact on performance and provides no benefit.

Throwing exceptions can be very expensive, so make sure to minimize this cost.You can use Perfmon to check the number of exceptions your application is throwing.

The run time can throw exceptions on its own! For example, Response.Redirect() throws a ThreadAbort exception. Even if you don’t explicitly throw exceptions, you may use functions that do. Make sure you check Perfmon to get the real story, and the debugger to check the source.

Bear in mind that this has nothing to do with try/catch blocks: you only incur the cost when the actual exception is thrown. Make sure that never use exceptions to control the application flow,instead check for the condition which causes the exception.

3) Keep DOM Updates to a Minimum

To do anything interesting, your code has to modify the DOM. However keep in mind that changes to the DOM are extremely expensive. Understandably, the browser has to do quite a bit of work to handle calls to the DOM API that change the content of the page. Even more surprisingly, calls to methods that do not change the page can be particularly slow, as, for example, registering listeners.
Also beware of code that iterates over a large number of DOM objects, such as code that iterates over document.all to look up a particular element, or uses document.getElementByName(). When using
those, as the size of your page grows so will the time taken by your code to execute.

4) Handling Memory Leaks

Event listeners are a major cause of memory leaks, both on Internet Explorer and Firefox, because event listeners routinely refer to other elements. The way to get around this problem is to keep track of every event listener you register and to unregister all those listeners when the user navigates away from the page, thus breaking the loop and allowing the garbage collector to clean up afterwards.

On Firefox, the Leak Monitor extension will help you detect memory leaks in your pages. When installed, as you navigate away from a page, the extension will display a pop-up with the list of leaked objects for that page. If no pop-up is displayed, it means that no memory leak was caused by that page.
You can download and install the Leak Monitor extension from http://addons.mozilla.org/firefox/2490

5) Cache Aggressively

There are several features and tools in ASP that you can make use of to gain performance.
Output Caching—Stores the static result of an ASP request. Specified using the directive:

Duration—Time item exists in the cache
VaryByParam—Varies cache entries by Get/Post params
VaryByHeader—Varies cache entries by Http header
VaryByCustom—Varies cache entries by browser
Override to vary by whatever you want:
Fragment Caching—When it is not possible to store an entire page (privacy, personalization, dynamic content), you can use fragment caching to store parts of it for quicker retrieval later.
a) VaryByControl—Varies the cached items by values of a control

Cache API—Provides extremely fine granularity for caching by keeping a hashtable of cached objects in memory (System.web.UI.caching). It also:
a) Includes Dependencies (key, file, time)

b) Automatically expires unused items

c) Supports Callbacks

Caching intelligently can give you excellent performance, and it’s important to think about what kind of caching you need.

Advertisements

Tips and Tricks :Visual Studio .Net IDE

Tips and Tricks :Visual Studio .Net IDE

A collection of tips and tricks, features, for the Visual Studio .NET IDE.

Read from the book Best Kept Secrets in Dot NET from the APress publishing house.

1. Multiple copy/pastes

Ctrl+Shift+V cycles through the clipboard ring. You can copy/cut multiple times from one area of code, then go to another area and paste them one after another.

2. Previous cursor positions

Ctrl+ - i.e. Ctrl + Hyphen. This cycles you through the code positions you visited.

Ctrl+Shift+- to navigate in the opposite direction.

3. Incremental search

To incrementally search for text as you type, first press Ctrl+i. Then type the word you want to search. Hit backspace to clear a character and enter to finish. Pressing F3 after this will work as usual, i.e. search for the next occurrence of previous search.Ctrl+Shift+i is used for Reverse Incremental Search.

4. Matching brace/comment/region/quote

Ctrl+] takes you to the matching brace. It also takes you to the matching comment, region or quote depending on what is at the cursor now.

5. Vertical block selection

Press Alt and then select the area you want with your mouse.this is very useful when you have to delete line numbers or some common unwanted text in all the lines,like for eg. Using a WITH block and removing the object name from multiple lines of the code.

6. Closing/Showing support windows

There are a bunch of necessary/useful windows in the Visual Studio IDE like Properties (F4), Solution Explorer (Ctrl+Alt+L), Output Window (Ctrl+Alt+O), Task List (Ctrl+Alt+K) etc. However, they take up a lot of space. An easy way around this is to use the auto hide feature.

Open the window you want. Right click on its title and choose Auto Hide. The window will dock in whenever your mouse is not hovering over it.

7. Track things you have to do with Task List

The Task List window (Ctrl+Alt+K) allows you to keep track of the things you have to do. Right click on the Task List window and choose Show Tasks|All to see a list of tasks. Ctrl+Shift+F12 to cycle through your list of tasks.

By default, comments marked with a TODO will appear in the task list.

8. Edit Task List Comment Tokens

You can add your own set of comment tokens (like the TODO comment token). Goto Tools|Options|Environment|Task List|Comment Tokens and make your changes. You can change the priority appearance of each comment token too.

9. Add Task List Shortcut

Add a shortcut to the task list with Ctrl+K, Ctrl+H. This will add the current line to the task list.

10. Auto-complete (Almost everyone knows this one)

Press Ctrl+Space or Alt+RightArrowto auto-complete the word. Intellisense suggestions may pop up a window if there is more than one possibility.

11. Intellisense suggestions window

Press Ctrl+Shift+Space to bring up the intellisense suggestions window. When giving parameters for functions, I often need to escape the suggestions window to check another part of code. To bring it back, I used to delete a comma and then type it again; but this is easier.

12. Line numbering

Tools|Options|Text Editor|All Languages|General|Line numbers.

If you want to set this option for only one language, then choose the appropriate language instead of All Languages.

13. Bookmarks

Bookmarks are available through Edit|Bookmarks. Bookmarks allow you to mark places in your code that you would want to come back to.

  • Create/Remove Bookmark – Ctrl+K, Ctrl+K
  • Move to next bookmark – Ctrl+K, Ctrl+N
  • Move to previous bookmark – Ctrl+K, Ctrl+P
  • Clear all bookmarks – Ctrl+K, Ctrl+L

14. Code Formatting

  • Auto-format selection – Ctrl+K, Ctrl+F
  • Convert to lower case – Ctrl+U
  • Convert to upper case – Ctrl+Shift+U
  • Comment selection – Ctrl+K, Ctrl+C
  • Uncomment selection – Ctrl+K, Ctrl+U

15. Outlining

I like this feature that allows me to hide code that is irrelevant to what I’m currently working on.

  • Fold/Unfold the current code block – Ctrl+M, Ctrl+M
  • Unfold all – Ctrl+M, Ctrl+L
  • Stop outlining – Ctrl+M, Ctrl+P
  • Fold all – Ctrl+M, Ctrl+O

16. Build and Debug

  • Build – Ctrl+Shift+B
  • Run – Ctrl+F5
  • Debug – F5

17. Wrapping Code

If you have long code lines, you’ll find that you do a lot of horizontal scrolling to view the entire line. As an alternative, you can wrap the code lines so you can view the code without needing to scroll.The easiest way to toggle word wrapping is by pressing Ctrl+R and then Ctrl+R again or by selecting Edit –> Advanced –> Word Wrap from the menu. When word wrapping is turned on, the horizontal scroll bar is removed and any lines of code that exceed the width of the code window will automatically wrap to the next line. This wrapping does not require any additional line-continuation characters (for VB), nor does it affect how the code is compiled.