Well I have now compared Mono.CSharp
with Microsoft.CSharp
and the result is that Mono's version performs badly in comparison.
I compiled the following code with both dynamic compilers:
using System;
using CompilerServiceTest;
public class LalaDynamicImpl : ILala
{
private static int _counter;
public void DoLala()
{
_counter++;
}
}
The main program looks something along the lines of:
public interface ILala
{
void DoLala();
}
public class LalaStaticImpl : ILala
{
private static int _counter;
public void DoLala()
{
_counter++;
}
}
public class Program
{
public static void Main(string[] args)
{
Message("Compiling dynamic lala...");
var lala = BuildDynamicLala();
Message("Testing dynamic lala...");
Test(lala);
Message("Sleeping for 1s...");
GC.Collect();
Thread.Sleep(1000);
Message("Testing static lala...");
Test(new LalaStaticImpl());
}
private static void Test(ILala lala)
{
var watch = Stopwatch.StartNew();
for (var i = 0; i < 1000000000; i++)
lala.DoLala();
Console.WriteLine(watch.Elapsed);
}
}
On my machine the results are:
- Statically compiled class: 1.9s
- Dynamically compiled class compiled with
Microsoft.CSharp
: 1.9s
- Dynamically compiled class compiled with
Mono.CSharp
: 10s
The results correspond with what the Mono team mentions here and what Marek Safar mentions in his answser - the Microsoft.CSharp
compiler service is a wrapper for the actual compiler and the code is compiled and optimized in the same way as normal code. The Mono.Csharp
compiler service is separate from the Mono compiler and is more of an "eval" machine. It does not optimize the code in the same way as the standard compiler.
Basically it appears that the Mono compiler service isn't really intended for performance-critical scenarios, but for rich features, learning, testing, etc.
Tested on a Windows 7 64bit machine with .NET 4.0 (VS 2010) and Mono 3.2.3.