CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeffrey Palermo (.com)

Blog moved to www.jeffreypalermo.com

There is NO performance different between code-behind and in-line code. - level 200

I recently had an interesting discussion over lunch with a colleague about the performance of in-line code versus the code-behind model.  I relayed that I prefer to put the small, UI-related code in a <script runat=”server”/> block in the .aspx file, and my colleague swore by the code-behind model.  I currently use the code-behind model because intellisense makes development so much faster, but every time I want to make a small change to the UI that involves som C#, I have to fire up VS.NET and then build the whole project.  For my corporate projects I do this anyway because the of the source control, but not all projects need this.  My colleague insisted that script block incur and extra performance hit because the ASP.NET engine must check for changes in every page hit.  After lunch, I whipped up a test and created to ASP.NET pages:  one with code-behind, and the other with the code in a script block.  I ran each for 1 minute in WAST.  Then I ran each 8 more times.  My results:  It doesn't make a hill of beans difference.  Each test was less than 1% difference.  Some tests the code-behind was less and 1% faster and some resulted in the in-line code being less than 1% faster.  Averaging all the tests gave the same performance for both methods.  So now I know first-hand that it doesn't matter architecturally.  It's only personal  and team preference.  Now, don't take this as an excuse for putting a lot of code in your .aspx page.  If you choose to do that, it should only be UI event handlers.  All REAL code should be in your business objects.  Use this knowledge responsibly!


Comments

Steve Hebert said:

In your post, you stated... "every time I want to make a small change to the UI that involves som C#, I have to fire up VS.NET and then build the whole project"

In your <%@ Page ... %> tag, you can replace Inherits= with src= to get around this. This means you have to distribute your C# code, but there is the workaround if you need it.

If you want a rundown on what's happening on code-behind vs. in-script, check out Chapter 1 on Essential ASP.NET by Fritz Onion. You'll understand exactly why there is no difference and then you can hand it to your buddy.

Then I'd run the following code:
colleague.SpeaksFromSphincter = true;

# June 2, 2004 2:54 AM

Jeffrey Palermo said:

Explanation of dynamic ASPX compilation and recompilation - level 300
# June 2, 2004 4:30 AM

Rob said:

From MSDN, ASP.Net Common Performance Myths: http://msdn.microsoft.com/msdnmag/issues/05/01/ASPNETPerformance/default.aspx?side=true

Steve, I think you meant to say "replace Codebehind= with src= ". Inherits= must be used with src=.
# March 4, 2005 5:30 AM

Rob Conery » Crazy Talk: Inline Scripting and Code-Behind said:

Pingback from  Rob Conery &raquo; Crazy Talk: Inline Scripting and Code-Behind

# October 15, 2007 6:06 PM

SergioTarrillo's RichWeblog said:

De ahora en adelante [PLEFDS = para leer el fin de semana]. Digamos que hay dos maneras de tener el c&#243;digo

# February 21, 2008 1:30 PM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

Headspring Systems

View Jeffrey Palermo's profile on LinkedIn

See my new blog at .jeffreypalermo.com