.NET Code Performance
November 22nd, 2006 // 8:11 pm @ Amar
I recently had been busy performance tuning .NET code, and I was very surprised at some of the findings as I timed and profiled various code modules. I had done a lot of performance tuning with C++ before I migrated to .NET. Had not done any serious performance tuning till now with .NET. While I was profiling a SharePoint WebPart, these findings would be equally applicable to any .NET code.
I was surprised to see that accessing a property off an object had a surprising big hit. e.g.
MyObject is an object which has a property say, ID.
I have a loop of code which uses the MyObject and tried to compare the ID with something…
for (int i =0; i < 100; i ++)
{
if ( MyObject.ID == something)
do something…
}
I observed that there were a few milliseconds being spend whenever I tried to access MyObject.ID.
Alternatively the following code ran much faster.
int id = MyObject.ID
for (int i =0; i < 100; i ++)
{
if ( id == something)
do something…
}
I guess the overhead of accessing the object and getting the property value does have a hit and can take up time if say you have a recursive function or an iterative function. Will be careful of such cases in the future.
I guess I will spend a bit more time exploring the internals of .NET to profile and find out such cases which can make a difference in our code.
P.S. I know I have not been posting to this blog for a bit. I am trying to move my blog to a better host and with better control over it. Yahoo Hosting is not adequate for it, as I cannot do a lot of things I plan to do with my Wordpress installation. Hence will mostly be moving this site to a new server which does not have restrictions. So busy evaluating offers and packaged from hundreds of hosting companies to find one which is just right for a geek developer like me, who would like to have enough control when needed from time to time.
Category : SharePoint & dotNet