Bruce Zhang

<August 2008>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456


Navigation

.Net Resource

Favourite Blog

Subscriptions



Debug And Release in Visual Studio.Net 2003®

Don't forget to change the debug mode to the release in Visual Studio.Net 2003® before you compile and publish your project.

I didn't realize the difference between these until I read the K. Scott Allen's article which title was Get a Charge From Statics with Seven Essential Programming Tips, published on the MSDN Magazine January. He intruduced some tips of the statics in .Net. One of these, if you want to initialize the static field, there are two scenarios: “The first scenario is when a developer explicitly adds a type constructor using the Shared keyword on a New subroutine in Visual Basic, or by adding a static, parameterless method with the same name as the type in C#. The second scenario is when a type has a initializer for a static field, in which case the compiler adds a type constructor behind the scenes.”[Scott]

There was an example in his article written by VB.Net, and I translated it into C#. The source code is following:

using System;
namespace Exceptions
{
 class Class1
 {
  private static int max = Int32.MaxValue - 1;
  [STAThread]
  static void Main(string[] args)
  {
            TestStaticPropertyAccess();
            Console.ReadLine();  
  }

  static void TestStaticPropertyAccess()
  {
   DateTime begin = DateTime.Now;
   for (int i=0;i<max;i++)
   {
    string message = ExplicitConstructor.Message;
   }
   PrintResult(DateTime.Now.Subtract(begin),"ExplicitConstructor");

   begin = DateTime.Now;
   for (int i=0;i<max;i++)
   {
    string message = ImplicitConstructor.Message;
   }
   PrintResult(DateTime.Now.Subtract(begin),"ImplicitConstructor");
  }

  static void PrintResult(TimeSpan span,string result)
  {
   Console.WriteLine("{0} took {1} ms.",result,span.Milliseconds);
  }
 }

class ExplicitConstructor
 {
  private static string message;

  static ExplicitConstructor()
  {
   message = "Hello World";
  }

  public static string Message
  {
   get { return message; }
  } 
 }

 class ImplicitConstructor
 {
  private static string message = "Hello World"; 

  public static string Message
  {
   get { return message; }
  } 
 }

He told us: “the first loop (with ExplicitConstructor) executes approximately eight times slower than the second loop (with ImplicitConstructor).” So I tried it, but I got the opposite result. Why? What happen? I couldn't get the right answer, so I had to mail Scott. Soon he answered my stupid question. The reason was the difference between Debug and Release mode in Visual Stuio.Net 2003®.

His mail script is :

Hi Bruce:

I'm glad you enjoyed the article. Thank you for the kind note.

A couple thing to check:

1. Make sure you compile the program in release mode
2. Make sure to run the program outside of the IDE.

Those two steps make sure the program is in release and has full optimizations.

Oh, my god, I forget it can optimize the performance when your programs are in the release mode. Thank Scott!

posted on Tuesday, December 21, 2004 6:55 AM by wayfarer





Powered by Dot Net Junkies, by Telligent Systems