tags:

views:

1155

answers:

4

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();
    }
+19  A: 

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.

joemoe
@joemoe Thanks for the clarification. I will try other comparisons like I/O, database. Seems like Mono is optimized for Linux and .NET is optimised for windows. As Linux beats windows in performance in many ways it makes sense that Mono on Linux beats .NET on windows.
funwithcoding
Quote from mono mailing list"To clear things up, the IL code that Mono generates from the code sources is the same for all OSes, but the native code generated (JITed) from the IL code is what varies depending on the OS. And the generated native code is more or less efficient depending on the OS."
funwithcoding
another thing to note is that any operation which requires lots of memory allocation and garbage collection is slow on mono, they are however working on speeding this us using a new garbage collector.
trampster
The current garbage collector chokes in 2 major ways: (1) collection performance degrades linearly with # of objects on the heap. (2) excessive allocations/deallocations might lead to memory fragmentation. The new garbage collector mostly eliminates these problems.
joemoe
@joemoe, Re:collection performance degrades linearly with # of objects on the heap, I think you'd be hard-pressed to find a GC algorithm that wasn't linear in the # objects. The question is how low you can make the linear scaling factor on your GC algorithm.
naasking
+1  A: 

You're not really making much of a comparison here. You are basically comparing one function (Math.Pow) between the two. Presumably a real application will do more things than this one function.

Mono's Math.Pow looks to be optimized by implementing this in C. I do not know how .Net implements it. It may be implemented fully in managed code.

Likely you will find that both runtimes are fast enough for every day needs.

jpobst
@jpobst Thats the point I want to make, Not only Math.Pow, many math functions seems to be faster in Mono. Imagine an financial application which does lot of math operations or scientific applications,for them probably Mono serves better than .NET and that to with ease of programming.I completely agree that both runtimes are fast enough for every day needs. In fact for the most of Line of business applications, we dont care performance as much as we do development time, otherwise we wouldnt be using C# in first place, If it is only for performance we would have used C++, C or assembly.
funwithcoding
I wanted to understand why it is faster in Mono when both share the same IL. Now I realize that though the IL is same, OS is different and the way IL is converted to native code is different.
funwithcoding
A: 

Well, Linux is way faster then Windows in any way, so I'm not surprised it executes the same bytecode faster.

Besides, since Mone doesn't have huge MS marketing department behind his back, it actually has to be faster in order to be even considered as a viable alternative.

Btw, if you try Windows version of Mono, I would expect smaller performance difference, except in cases that involve SIMD optimized operations.

Dev er dev
@Dev yes Mono on windows is slow
funwithcoding
Mere subjective speculation.
spoulson
"Linux is way faster then Windows in any way" [citation needed]
musicfreak
Since when does a marketing department result in technical differences?
Arafangion
@Arafangion: It does. Money spent on marketing is money not spent on product itself. With good enough marketing, product doesn't have to be that good to be successful.
Dev er dev
@Dev: Oh, so now products have to be good to be successful, if they don't have good marketing?
Arafangion
A: 

Mono is very slow on Linux

Baurzhan
Very constructive. </sarcasm>
GaiusSensei