Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#
Tip/Trick

Roselyn CTP vs. Microsoft.CSharp (C# native compiler)

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
4 Sep 2012CPOL 17.4K   3   2
Comparison between the new Roselyn CTP compiler from Microsoft and the native C# compiler which is integrated into .NET.

Here is a small comparison between the new Roselyn CTP compiler from Microsoft (http://www.microsoft.com/en-us/download/details.aspx?id=27746) and the native C# compiler which is integrated into .NET.

Here is the code used for testing compilation between the "two" compilers:

C#
static class Program
{
    static void Main()
    {
        int pass = 50;
        List<long> rList = new List<long>();
        List<long> oList = new List<long>();
 
        for (int i = 1; i < pass; i++)
        {
            long roselynVal = TestRoselyn();
            long oldVal = TestOld();
 
            rList.Add(roselynVal);
            oList.Add(oldVal);
 
            Console.WriteLine("Roselyn Pass " + i + ":  " + roselynVal.ToString() + 
                     "\t\tOld Microsoft compiler Pass " + i + ":  " + oldVal.ToString());
        }
 
        var rosleynAvg = rList.Average();
        var oldAvg = oList.Average();
        //var rAvg = from r in rList
        Console.WriteLine("Roselyn average ms:\t" + rosleynAvg);
        Console.WriteLine("Microsoft old compiler average ms:\t" + oldAvg);
 
        Console.ReadKey();
    }
 
    /// <summary>
    /// Test roselyn
    /// </summary>
    /// <returns></returns>
    private static long TestRoselyn()
    {
        //Crate stopwatch and evaluate script using Roselyn
        Stopwatch sw = new Stopwatch();
 
        sw.Start();
        var engine = new ScriptEngine();
        var result = engine.Execute<bool>("var x = 10; x == 10");
        sw.Stop();
        return sw.ElapsedMilliseconds;
    }
 
    /// <summary>
    /// Test old one
    /// </summary>
    /// <returns></returns>
    private static long TestOld()
    {
        Stopwatch sw2 = new Stopwatch();
 
        sw2.Start();
        Microsoft.CSharp.CSharpCodeProvider cp = new CSharpCodeProvider();
        CompilerParameters cparam = new CompilerParameters { GenerateInMemory = true };
        CompilerResults cr = cp.CompileAssemblyFromSource(cparam,
           "namespace RoselynTemplate { class Main { public bool " + 
           "GetSmt() { var x = 10; return x == 10; } }}");
        var instance = cr.CompiledAssembly.CreateInstance("RoselynTemplate.Main");
        var method = instance.GetType().GetMethod("GetSmt");
        object res = method.Invoke(instance, null);
        sw2.Stop();
        return sw2.ElapsedMilliseconds;
    }
}

And here are the test results:

Image 1

Considering code execution, it seems that Roselyn uses some kind of caching mechanism, also I need to mention that every time at “Pass 1” the old Microsoft compiler has beaten Roselyn by 6-7ms which I presume is spent on caching and other operations.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
John B Oliver2-Oct-12 12:00
John B Oliver2-Oct-12 12:00 
GeneralRe: My vote of 2 Pin
Djenad3-Oct-12 3:46
Djenad3-Oct-12 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.