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();
}
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.
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.
All this really tests is the implementation of the timer - which are typically bad tests because no two OS implement them the same. Some use a hard timer which is very exact (Linux) and some have a timer than can be preempted (Windows) based on system needs. If you want to benchmark a call you have avoid intermediate calls or you are probably timing them not what you are thinking you are testing. Math POW is going to be nearly the same on both platforms since the math to calculate it hasn't changed in a hundred years. I learn toward OS differences with the timer more than anything.
Well, I'm not surprised it executes the same bytecode faster.
If you try the Windows version of Mono, I would expect a smaller performance difference, except in cases that involve SIMD optimized operations.
精彩评论