Mono Project: Why is mono faster than .NET?

funwithcoding picture funwithcoding · Mar 7, 2010 · Viewed 21.8k times · Source

I am surprised to observe that mono is faster than .NET. Does anyone know why is it so? I was expecting mono to be slower than .NET but wasnt the case atleast with my experiments.

I have a windows xp laptop with .NET framework. I am running CentOS on vmware vmplayer on top of windows xp. I wanted to try mono. So grabbed the Mono 2.6.1 sources and installed it on CentOS in vmplayer. I have writen a test webservice application using .Net 2.0 executed it on wndows, it worked, I transferred the binary to the centos in the vmplayer without any recompilation and executed it on centos. Hurray it worked! Life is good but then something else lured my attention. The execution of the application on centos seemed to be faster. I did not believe my eyes.

To confirm my observation, I eliminated network out of the equation because network reponse depends on lot of external factors.

I grabbed small dummy loop code from internet, compiled it in visual studio executed in windows as well as CentOS, the results are as follows

Output on windows console is HelloConsole\bin\Debug>HelloConsole.exe
Result =2.66666664666712E+24
37443.6077769661 ms


Output on Centos console is [rupert@bagvapp Math.Pow]$ mono HelloConsole.exe
Result =2.66666664666712E+24
28790.6286 ms

If anyone can explain this behavior,that would be great. my layman's understanding is Mono's implementation is more efficient than .NET framework. Even if I assume Mono's Math implementation is only efficient. But lot of implementations like processing financial data, graphics calculations depend lot on Math library. It would be more interesting to perform the test on Mono/Centos directly without vmware but that needs some time. I will give it a try may be next weekend.

public static void DummyLoop()
    {
        double sumOfPowers = 0;
        int count = Convert.ToInt32(ConfigurationManager.AppSettings["count"]);

            for (int i = 0; i < count; i++)
            {
                sumOfPowers += Math.Pow(i, 2);   
            }


        Console.WriteLine("Result =" + sumOfPowers);   
    }


    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        DummyLoop();
        stopWatch.Stop();
        double ms = (stopWatch.ElapsedTicks * 1000.0) / Stopwatch.Frequency;
        Console.WriteLine(string.Concat(ms.ToString(), " ms"));
        Console.ReadLine();
    }

Answer

joemoe picture joemoe · Mar 7, 2010

This was a discussion about this on the Mono mailing list not too long ago. The reasoning they provided is Mono is extremely optimized under Linux (exactly how did not seem to be mentioned). But Mono is not the only variable here, if you are running Mono on Linux. It's a different OS, different process schedulers and various kernel level subsystems can have a markedly different effects on performance, independent of the Mono team's work. Mono also makes heavy use of compiler intrinsics, which .NET may or may not do.

However, most of the time on a Mono on Windows vs .NET on Windows comparison, you might find .NET beating Mono more often then not. But even then, Mono might not always lose. In fact, for code specifically optimized for Mono (eg: using Mono.SIMD), you might get an order of magnitude better performance on Mono vs .NET, regardless of platform.