Andrei Rinea

.NET Framework & SQL Server

Request.UserHostAddress versus Request.ServerVariables["REMOTE_ADDR"]

clock February 10, 2010 08:59 by author Andrei Rinea

I just found out a HUGE performance difference between these two. Tested on my cheap laptop (HP Compaq 615 : AMD Turion Dual core 2,2GHz, 3GB RAM, 7200 RPM HDD; 500$) on .NET 4.0 Beta2 after several test runs (averaged results), 1 million iterations, release mode and Stopwatch for time measurement (as opposed to DateTime.Now) :

var a = Request.UserHostAddress; // -> 240 microseconds

var a = Request.ServerVariables["REMOTE_ADDR"]; // -> 0,4 microseconds

What the f...rick?! 500x times faster?

What does Request.UserHostAddress to use so much more time? Let's fire up good ol' Reflector :

	public string get_UserHostAddress()	
	{	    
	if (this._wr != null)	    
	{	        
	return this._wr.GetRemoteAddress();	    
	}	    
	return null;	
	}	
	

_wr is a HttpWorkerRequest (a public abstract class). The GetRemoteAddress is also abstract so I can't guess from Reflector. So I fired up Visual Studio 2010 again and saw that _wr was a Microsoft.VisualStudio.WebHost.Request instance. So much for my astonishment.. If it's hosted in the IDE it can be slow..

I still ponder if it's worth testing in a production environment whether it's faster or not (Request.UserHostAddress) significantly or not..

 

EDIT1 : Couldn't wait to test this at work under IIS 6.0 (Windows Server 2003, Intel Core2Duo 2,4 GHz, 4GB RAM) and Request.UserHostAddress takes 86 nanoseconds to be queried... Can't wait to get home and test again on IIS 7.5 (Windows 7 Ultimate RC)

 EDIT2: At home on IIS7.5, same laptop, release mode etc. etc. 29 microseconds for Request.UserHostAddress. It's better than Cassini's 240 microseconds but it should have been at most 100 nanoseconds...that's one thousand times slower..



Tip : Middle click on tab

clock August 16, 2009 20:18 by author Andrei Rinea

Did you know that middle-clicking (that is clicking on the mouse's middle button - usually the wheel) on a tab in most decent software applications will close the tab? Here's a small list of applications which honor this :

  • Most main browsers (IE 7+, Opera 7.x+, Firefox 2.x+, Chrome 1.x+ - yes, Safari - doesn't)
  • Visual Studio
  • RSS Bandit (my RSS Reader of choice - have tried Google Reader and Feed Reader in the past but no thanks)

Why would this be useful? Because CTRL-F4 or searching for the "X" closing button might take more time and/or precision than middle clicking anywhere on the tab label up there :)