Tuesday, June 30, 2009

Best practices to Improve your .net codes performance

The below mentioned practices can Improve your .net codes performance

1. DateTime.Now() method is commonly used to get the current time.
The value returned by the Now() method is in the current machine
time-zone context. A common practice is to convert time that is going
to be stored or sent between machines into Universal (UCT) time.
When daylight savings time is a possibility, this can introduce bugs.
For example –

DateTime date = DateTime.Now().ToUniversalTime();

The value that results from the above code will be off by an hour if
called during the extra hour that occurs during the daylight savings
time switch in the fall. To fi x this, a best practice is to call DateTime.
UtcNow()
instead of calling DateTime.Now(), and then converting it
to universal time.

DateTime date = DateTime.UtcNow();

This code will always have the 24-hour-day perspective, and may
then be safely converted to local time.


2. When using Add/Subtract functions with DateTime, take into account
the daylight savings time.

For example –

DateTime dt = DateTime.Parse(“Nov 4, 2007 12:00:00 AM”);
dt = dt.AddHours(3.0);


The expected result will be 03:00:00 AM. This result may seem correct
at fi rst; however, on November 04, 2007, at 02:00 AM PST, the daylight
savings time change took effect. So the correct answer should have
been Nov 4, 2007, 02:00:00 AM. To avoid this, the following syntax
can be used:

dt = dt.ToUniversalTime().AddHours(3.0).ToLocalTime();

This converts the local time to UTC, performs the calculation, and
then converts back to local time.


3. Do not compare strings by converting them to uppercase or lowercase, use
String.Compare instead, which can ignore the case and compare.


4 .Avoid the use of ArrayList - Objects added to the ArrayList are added
as System.Object and when retrieving values back from the ArrayList,
these objects have to be unboxed to return the actual value type. So it is
recommended to use the custom typed collections instead of ArrayList.

For example - string objects can be stored in StringCollection
(System.Collection.Specialized)


5. Always catch the specifi c exceptions instead of generic System
Exception.


6. While copying data from one DataTable to another, the simple way
is to clone an existing DataTable, loop through all rows of source

DataTable and copy data column by column and add row to the
destination DataTable. But this is not a feasible solution if there are
millions of records.

Hence, a better way is to use the DataTable.
ImportRow()
method.

The ImportRow() method of the DataTable
copies a row into a DataTable with all of the properties and data of the
row.
It actually calls NewRow() method on destination DataTable with
current table schema and sets DataRowState to "Added".

Benefits

* Performance of the application can be improved.

* Some of these tips will give more accurate results.

* Quality of coding can be improved.

No comments:

Post a Comment