问题
I have an ASP.NET application that is working well on my Windows development machine. The server is Linux running Mono though, and once uploaded the same code is running 4 or 5 times slower there than it does on the Windows box (taking 25 seconds vs 5 seconds for one task for instance).
Is this performance a known problem with Mono ? And is there anything I can do about it ? The code is mostly text processing, string replaces, regexes and the like, if that makes any difference. I've profiled and debugged my code using VS locally, but I don't know if it's possible to do remote debugging on the server with Mono, or what I need to do next to fix it really.
回答1:
Regexes are a particularly weak area for Mono. Mono's Regex class always uses interpreted code, while .Net can turn it into compiled IL, resulting in much faster execution.
Most other forms of text processing like replaces should be roughly similar.
回答2:
Install Mono, preferably on a Linux system similar to your server. Profile your code on Mono and see where the bottlenecks are.
I have a Mono app running on a Linux server that follows Apache log files. I developed it on Windows and when testing it on Linux, I found it to be something like 8-10 times slower on Mono 2.4 vs. .NET 3.5. Most of its time is spent in Regex.Match and string functions. I was able to double the overall speed of the program in Mono just by specifying StringComparison.Ordinal in 4 calls to string.EndsWith(). If ordinal string comparisons are what you want, that might give you a speed boost.
Even with StringComparison.Ordinal, string.StartsWith() was still slow. I got a 25% increase in overall program speed by writing my own version of string.StartsWith().
So if ordinal comparisons are what your app needs to do, try specifying StringComparison.Ordinal or writing your own string functions.
回答3:
Are you using mod_mono or mod_proxy for this? While there are limitations to what you can get out of Mono, you're also going to get less general delay using mod_mono than mod_proxy.
Please see the section "mod_mono and mod_proxy" at Mono ASP.NET FAQ
回答4:
Are you using StringBuilder objects, or are you doing string catenation. If you are doing lots of string work you will hit some performance bugs.
But I think leppie has it on the head, Microsoft spent a lot of money testing and integrating ASP.net into IIS so that it was really, really fast. If you don't want to pay for the MS box, then you are going to have to deal with the fact that Mono is open source and IIS is a tested commercial product.
A good example is here, where Microsoft actually modified Windows itself with a kernel mode library that works with the IIS server. Correctly architected apps using the cache properly can get very good performance increases in this code: HTTP.sys
回答5:
According to this academic study, Mono performance can be hampered in some Linux distributions due to some Linux systems' "much quicker propensity to go to the hard drive cache/swap space" [when compared to Windows].
"Please note the vast difference indicated in the two operating systems even utilizing native code. At 131072 integers in the array the Fedora Core 4 Test 3 operating system performed more than 3 times slower than the exact same code in Windows."
As Linux distribution kernel scheduling configurations vary, it would be useful to know what OS distribution and version you're running on your Linux server, and how much memory and CPU cycles are available to your application.
来源:https://stackoverflow.com/questions/4755707/c-sharp-code-runs-quick-on-iis-but-slow-on-mono-how-to-improve-it