Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# code runs quick on IIS, but slow on Mono - how to improve it?

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.

like image 922
Michael Low Avatar asked Jan 21 '11 05:01

Michael Low


3 Answers

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.

like image 120
jpobst Avatar answered Oct 14 '22 01:10

jpobst


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.

like image 4
Greg Najda Avatar answered Oct 14 '22 01:10

Greg Najda


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

like image 1
Mblake Avatar answered Oct 14 '22 01:10

Mblake