I don’t claim to be the best developer on the planet :) but I’m always amazed at how all the “experts” come out of the woodwork to posh on other peoples code :)

Several folks have commented and emailed about hey the technique I posted earlier was unnecessary. But only ONE guy (thanks Henrik Juhlin) took the time to research before commenting.

Since the last company that I built did nothing but performance work, it did get me thinking, so I built a little test.

I used 4 potential access methods and ran the test with debugging OFF!

Here are the results.

Retrieve and Assign 200,000 Times

From Application Object : 1797 Milliseconds

From Config File : 1699 Milliseconds

From Resource File : 684 Milliseconds

From Static Variable : 5 Milliseconds

In a real production environment there are, of course, additional factors to consider, but from a pure retrieval perspective, that makes the Static Instance property like 340 TIMES faster than reading from the config file.

Here is the code.

   1:  static public string MySiteName { get; set; }
   2:    
   3:  protected void Page_Load(object sender, EventArgs e)
   4:  {
   5:      Application["SiteName"] = "This is s Test";
   6:      MySiteName = WebConfigurationManager.AppSettings.Get("SiteName");
   7:   
   8:   
   9:  }
  10:   
  11:  protected void Button1_Click(object sender, EventArgs e)
  12:  {
  13:      
  14:      string MyString = "This is a string test ";
  15:      long StartTime;
  16:      long EndTime;
  17:      long ElapsedTime; 
  18:      
  19:      // App Setting 
  20:      StartTime = DateTime.Now.Ticks;
  21:      for (long i = 0; i < 2000000; i++)
  22:      {
  23:          MyString = WebConfigurationManager.AppSettings.Get("SiteName");
  24:      }
  25:      EndTime = DateTime.Now.Ticks;
  26:      ElapsedTime = EndTime - StartTime;
  27:      ElapsedTime = ElapsedTime / 10000;
  28:      Label1.Text = ElapsedTime.ToString();
  29:   
  30:      // Resource File  
  31:      StartTime = DateTime.Now.Ticks;
  32:      for (long i = 0; i < 2000000; i++)
  33:      {
  34:          MyString = GetLocalResourceObject("Resource1.Text").ToString();   
  35:      }
  36:      EndTime = DateTime.Now.Ticks;
  37:      ElapsedTime = EndTime - StartTime;
  38:      ElapsedTime = ElapsedTime / 10000;
  39:      Label2.Text = ElapsedTime.ToString();
  40:   
  41:      // Application Object 
  42:      StartTime = DateTime.Now.Ticks;
  43:      for (long i = 0; i < 2000000; i++)
  44:      {
  45:          MyString = Application["SiteName"].ToString();
  46:      }
  47:      EndTime = DateTime.Now.Ticks;
  48:      ElapsedTime = EndTime - StartTime;
  49:      ElapsedTime = ElapsedTime / 10000;
  50:      Label3.Text = ElapsedTime.ToString();
  51:   
  52:      // Static Variable  
  53:      StartTime = DateTime.Now.Ticks;
  54:      for (long i = 0; i < 2000000; i++)
  55:      {
  56:          MyString = MySiteName;
  57:      }
  58:      EndTime = DateTime.Now.Ticks;
  59:      ElapsedTime = EndTime - StartTime;
  60:      ElapsedTime = ElapsedTime / 10000;
  61:      Label4.Text = ElapsedTime.ToString();
  62:  }

 

Technorati Tags: ,,
Article Global Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google Yahoo Buzz StumbleUpon Eli Pets