Posts

Showing posts from August, 2007

The ASP.NET Page Cycle

Being a consultant, I have worked with a lot of other consultants - professionals within their domain. It has struck me how few of the ASP.NET developers have a clear understanding of the ASP.NET page cycle, something I think is crucial to be able to write effective code. Given this experience, I have compiled a quick cheat-sheet that illustrates some basic, important, points. Please note, however, that what you find below is a non-exhaustive, simplified, list. The below page sequence is valid for instances of Control (Page inherits from Control): Page_PreInit Page_Init Page_PreLoad Page_Load EVENT HANDLERS , eg. Button1_Click If Page.Visible Then Page_PreRender This means, that Page_Load always runs. If you have code that decides whether or not a page/part of a page/control is shown that is dependant on another control, put that code into Page_PreRender. Example: Public Sub Page_Load( ... ) If Not IsPostBack Then LoadRegionalData() End If End Sub Public Sub Button1_

A scary realization, part 2

Image
OK, I just couldn't drop this subject until I'd done some more research. The image below, illustrates what I found: The application concatenates a bunch of strings, using different methods. Don't bother analyzing the actual individual values, but rather the relation between them: StringBuilder is fastest, most efficient without doubt. NO garbage collection was needed while processing the SAME strings, while all the other methods caused 2001 cleanups of the objects in Generation 0 (ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_GC_CollectionCount_1_804c5d7d.htm) String.Concat uses slightly less memory than &-concatenation, but takes up about the same CPU time. Using the StringBuilder.AppendFormat method, uses almost double the CPU time in comparission to StringBuilder.Append, and even more than using &-concatenation. Furthermore, this was the only method that caused recycling of objects in Generation 1. Using String.Format is the slowe

A scary realization

I wanted to find out how much faster using String.Format was, in comparison to just concatenating strings, using "&" ("+" in C#). To do so, I iterated a String.Format -statement and a equivalent concatenation statement 10'000'000 times, calculated the median and created a report. I did this a few times and found the following: When combining three strings (e.g. "Hello " & strFirstName & "!") String.Format used 96% more memory, taking up 50% more processor time than an &-concatenation. String.Concat used 16% more memory, taking up virtually the same processor time (1 ms more) as an &-concatenation. When combining seven strings String.Format used 10% less memory, taking up 59% more processor time than an &-concatenation. String.Concat used 2% less memory, taking up 3% less processor time than an &-concatenation. Conclusion When doing small concatenation, use "&" or "+". When