Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, October 13, 2010

Linq Lamba Expressions Performance Problems SingleOrDefault Super Slow

A few months ago a release my website www.puertovallarta-bienesraices.com for real estate properties in Puerto Vallarta, Jalisco Mexico. In the last few nights I decided to start optimizing the site and noticed huge performance problems when using SubSonic 3 with Linq. I used to use SubSonic 2 on a high traffic site without any problems. However, with Linq I have been having High CPU Peaks and Memory consumption. I throw some webtests/Load tests with Visual Studio 2010 and started noticed the performance issues especially with Linq. No problems with Subsonic since its just a layer to Linq to Sql.

In particular I was surprise with the function .SingleOrDefault() on a single call for a Property it was taking 0.2 seconds on a database with ~4,000 records. The database has already been optimized.

Photobucket

I did some reading and found an article from JD in regards to this issue
I decided to implement on the Property class the static for delegates:


public static Func> _propertyGetQuery =
SubSonic.Linq.Structure.QueryCompiler.Compile((PropertyRentalDB db, int id) =>
from p in db.Properties
where p.PropertyId == id
select p);

public static Property GetById(int propertyId)
{
var db = new PropertyRentalDB();
Property p = _propertyGetQuery(db, propertyId).SingleOrDefault();
return p;
}


Compile and run Ants performance profiler and the load tests and the performance results here HUGE!

Photobucket

Total hits 71. Average from 0.20 to 0.011.

Now I can go and optimized more of the code where I use similar instances.
However, I think on my next project I will just go back and use SubSonic 2 and forget about Linq.

Tuesday, July 22, 2008

Time Ago in Words for .NET C#

I saw the need for a C# version of the time_ago_in_words function in Ruby. I translated the code below into C# from my existing post I published for Ruby.





static string TimeAgo(DateTime time)
{
DateTime startDate = DateTime.Now;
TimeSpan deltaMinutes = startDate.Subtract(time);
string distance = string.Empty;
if (deltaMinutes.TotalMinutes <= (8724 * 60))
{
distance = DistanceOfTimeInWords(deltaMinutes.TotalMinutes);
if (deltaMinutes.Minutes < 0)
{
return distance + " from now";
}
else
{
return distance + " ago";
}
}
else
{
return "on " + time.ToString();
}
}


static string DistanceOfTimeInWords(double minutes)
{
if (minutes < 1)
{
return "less than a minute";
}
else if (minutes < 50)
{
return minutes.ToString() + " minutes";
}
else if (minutes < 90)
{
return "about one hour";
}
else if (minutes < 1080)
{
return Math.Round(new decimal((minutes / 60))).ToString() + " hours";
}
else if (minutes < 1440)
{
return "one day";
}
else if (minutes < 2880)
{
return "about one day";
}
else
{
return Math.Round(new decimal((minutes / 1440))).ToString() + " days";
}
}




You can download the complete source here: http://www.box.net/shared/3fmhllxq8g

Friday, June 27, 2008

Strip numbers from String : C# .NET Remove numbers from string

Here is a simple function I created to strip/remove numbers from a string.
I used the regular expressions to check if a char is number.
I choose to use regular expressions than using the Double.TryParse() because I feel RegEx are faster then the TryParse function. I could be wrong but at this point is doing the job.





public string StripNumbers(string input)
{
Regex regEx = new Regex("[0-9]+");
StringBuilder sb = new StringBuilder();
foreach (char a in input)
{
if (!regEx.IsMatch(a.ToString()))
{
sb.Append(a);
}
}

return sb.ToString();
}




The next steps will be to test performance and find out what method is faster to check for IsNumber().