I was curios how expensive is a cross domain operation so I have made a test.
The test procedure is simple. Perform a number of cross-domain and non cross-domain operations (get the value of NextNumber() from same domain and from another domain) and measure the time elapsed. For each value I have runned the application 3 times, recorded the time (in milliseconds) and created the mean of this three.
For this I’ve created a Console Application containing two classes, the main class and “NumberClass” which has a method that returns the next long. The code for this two is below:
NumberClass:
//MarshalByRefObject is used because this object will cross domain boundary class NumberClass:MarshalByRefObject { public ulong number = 0; public ulong NextNumber() { return number++; } }
The Main method from the main class:
static void Main(string[] args) { //Uncomment this two lines to load the class in another app domain //System.AppDomain appDom = System.AppDomain.CreateDomain("TestAppDomain"); //NumberClass n = (NumberClass)appDom.CreateInstanceAndUnwrap("AppDomain", "AppDomain.NumberClass"); NumberClass n = new NumberClass(); //Comment this line if uncomment the two above DateTime start = DateTime.Now; //We are going to change the value of i for (ulong i = 0; i < 10; i++) { n.NextNumber(); } DateTime end = DateTime.Now; TimeSpan elapsedTime = end - start; Console.WriteLine("Elapsed time: {0} ms", elapsedTime.TotalMilliseconds); Console.ReadLine(); }
The values of i are: 1, 10, 100, 1.000, 10.000, 100.000, 1.000.000 so a total of 42 measurements were performed. The test system is a Core2Duo 2Ghz, 2GB Ram 667 running Windows Vista Business x32 and Visual Studio 2008 Professional.
Let’s see the results:
Please note that the tallest bar from the left graph is almost the smallest from the right one :)
Conclusion: using AppDomains is cool, is useful but use them wisely because a large number of cross domain operations drastically affect performances.

