Coding optimization and performances


 

I am trying as much as I can how to optimize my code.
So I try to understand for example the performances issues between two ways of coding and of course which produce the same result.
For example, if I affect a background html color to a table I can write:

MyTable.Style.Add("background-color", Mycolor)

or I can do the same like that:

MyTable.BackColor = ColorTranslator.FromHTML(Mycolor)


Indeed I have the same questions for many little things, like searching for a control:
Is it better to use Findcontrol, or if the DOM never change, access it directly by its child index position like MyParent.Controls.Item(1) ?

All these questions, must have an impact on performances. I would like to know too is the CLR at the end don't bother and create the same code.

1 Comment

  • Indexed positions are always going to be faster than string comparisons, not matter what the context of the collection is. Still, even faster would be to use protected members of your class, which ASP.NET will automatically set if you are using usercontrol or web forms. The style code isn't going to make much difference either way, but I would recommend setting the actual property (ie. BackColor instead), since the webcontrols support down level rendering when you use these properties.





    If you are so concerned about performance that you need to be wondering about the impact of things like this, you might want to take a look at output caching or just bypass web controls all together and directly output HTML using the Render method instead.

Comments have been disabled for this content.